Open In App

JavaScript Program to Sort an Associative Array by its Values

Last Updated : 09 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Javascript, “arrays” are indexed by numerical values, while “objects” are essentially what you might refer to as associative arrays. It’s important to note that “objects” do not have a guaranteed order. In this article, we will learn how can we sort an associative array by its values.

Using sort() method

In this approach, we create a sorting function that has its own comparator which basically sorts the array based on the value property that we provide to it. This will help to achieve the sorting on the array we defined by making little changes in the structure.

Example 1: This example shows the use of the above-explained approach.

Javascript




let a = []; // Array
  
// Add elemets to it
a.push({ name: 'a', class: 1 });
a.push({ name: 'b', class: 9 });
a.push({ name: 'c', class: 2 });
a.push({ name: 'd', class: 6 });
  
// Custom sorting function
let sortedList = a.sort(function (a, b) {
    return a.class - b.class;
});
  
// Output
console.log(sortedList);


Output

[
  { name: 'a', class: 1 },
  { name: 'c', class: 2 },
  { name: 'd', class: 6 },
  { name: 'b', class: 9 }
]

Time Complexity : O(n), because it iterates over an array and compares over each of the entries of the array.

Space Complexity: O(1), because there is no extra space required and neither any recursive call.

Example 2: In this example, we will create array with the dynamic method and in the comparator function instead of substracting values we check for greater and compare.

Javascript




let list = new Array();
  
list.push({ key: 'a', value: 101 });
list.push({ key: 'b', value: 43 });
list.push({ key: 'd', value: 42 });
list.push({ key: 'k', value: 71 });
  
list.sort(function (a, b) {
  
    if (a.value > b.value) {
        return -1;
    }
    else if (a.value < b.value) {
        return 1;
    }
    return 0;
});
  
console.log(list);


Output

[
  { key: 'a', value: 101 },
  { key: 'k', value: 71 },
  { key: 'b', value: 43 },
  { key: 'd', value: 42 }
]

Time Complexity : O(n), because it iterates over an array and compares over each of the entries of the array.

Space Complexity: O(1), because there is no extra space required and neither any recursive call.

Using Object.entries method

In this approach, we will first convert the associative array into an array of key-value pairs using Object.entries. This will generate array of arrays in which sub-array contains a key-value pair. Then we will sort the array such that it sorts the array in ascending order based on the second element of each sub-array. Then use reduce function to convert the sorted array back into an object.

Example: This example shows the use of the above-explained approach.

Javascript




const a = {
    a: 90,
    e: 12,
    o: 27,
};
  
const arr = Object.entries(a);
  
arr.sort((a, b) => a[1] - b[1]);
  
const res = arr.reduce((acc, [key, value]) => {
    acc[key] = value;
    return acc;
}, {});
  
console.log(res)


Output

{ e: 12, o: 27, a: 90 }

Using object.key method

In this approach, we will first convert the associative array into an array of keys using Object.keys. This will generate array of keys. Then we will sort the array. Then use reduce function to convert the sorted array back into an object.

Example: This example shows the use of the above-explained approach.

Javascript




const arr = {
    b: 3,
    a: 1,
    d: 2,
};
const keys = Object.keys(arr);
  
const sarr = keys.sort((a, b) => arr[a] - arr[b]);
  
const obj = sarr.reduce((acc, key) => {
    acc[key] = arr[key];
    return acc;
}, {});
  
console.log(obj);


Output

{ a: 1, d: 2, b: 3 }


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads