How to Sort a Dictionary by Value in JavaScript ? Last Updated : 04 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, dictionaries do not have a built-in method to sort them by their values. However, there are several approaches to achieve this functionality. Below are the possible Approaches: Table of Content Using Array.sort() with Object.entries()Using a custom sort functionUsing Array.sort() with Object.entries()In this approach first, we convert the dictionary into an array of key-value pairs using Object.entries() then we sort that array based on the values, and then we convert it back into a dictionary using Object.fromEntries(). Example: The below example uses Array.sort() with Object.entries() methods to sort a Dictionary by Value in JavaScript. JavaScript const dict = { 'AI': 91, 'ML': 75, 'DSA': 88, 'IOT': 85 }; const sortedDict = Object.fromEntries( Object.entries(dict).sort(([,a],[,b]) => a- b) ); console.log(sortedDict); Output{ ML: 75, IOT: 85, DSA: 88, AI: 91 } Using a custom sort functionIn this approach, we are defining a custom sort function that sorts the dictionary based on their corresponding values. The custom function uses Object.keys() to get an array of keys and then sort the array based on the values. Finally, we will construct the dictionary using Array.reduce(). Example: The below example uses the Custom Sort Function to sort a Dictionary by Value in JavaScript. JavaScript function sortDictByValue(dict) { return Object.keys(dict) .sort((a, b) => dict[b] - dict[a]) .reduce((acc, key) => { acc[key] = dict[key]; return acc; }, {}); } const dict = { 'banana': 5, 'apple': 10, 'orange': 8, 'grape': 3 }; const sortedDict = sortDictByValue(dict); console.log(sortedDict); Output{ apple: 10, orange: 8, banana: 5, grape: 3 } Comment More infoAdvertise with us Next Article How to Sort a Dictionary by Value in JavaScript ? G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to sort a collection in JavaScript ? A javascript collection is much like a container. It's just an item that combines several elements into a single unit. Aggregate information is stored, accessed, modified, and communicated via collections. With the help of constructors, we create collections in javascript. In earlier versions of jav 3 min read How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort 3 min read How to Sort a Dictionary by Value in TypeScript ? In TypeScript, dictionaries are objects that store key-value pairs. Sorting a dictionary by value involves arranging the key-value pairs based on the values in ascending or descending order. The below approaches can be used to accomplish this task: Table of Content Using Object.entries() and Array.s 2 min read How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach 3 min read How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s 3 min read Like