Open In App

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:

Examples of Sorting a Dictionary by Value in TypeScript

Using Object.entries() and Array.sort() method

In this approach, we begin by converting the dictionary into an array of key-value pairs using Object.entries(). Then, we sort this array based on the values. Finally, we convert the sorted array back into a dictionary using Object.fromEntries().

Example: This example utilizes Array.sort() and Object.entries() methods to sort a Dictionary by its Values in TypeScript.

const dict: Record<string, number> =
 { 
   'Prasad': 21,
   'Saurabh': 23,
   'manohar': 19 
  };

const sortedDict1: Record<string, number> = Object.fromEntries(
    Object.entries(dict).sort((a, b) => a[1] - b[1])
);

console.log(sortedDict1);

Output

{ manohar: 19, Prasad: 21, Saurabh: 23 }

Using a custom sorting function

In this approach, we define a custom sorting function that sorts the dictionary's key-value pairs based on their values. This function uses Object.keys() to extract an array of keys and sorts it according to the corresponding values. Finally, we reconstruct the dictionary using Array.reduce().

Example: This example utilizes the Custom Sorting Function to sort a Dictionary by its Values in TypeScript.

function sortDictByValue(dict: Record<string, number>)
: Record<string, number> {
    return Object.keys(dict)
      .sort((a, b) => dict[b] - dict[a])
      .reduce((acc, key) => {
        acc[key] = dict[key];
        return acc;
      }, {} as Record<string, number>);
  }
  
  const dict: Record<string, number> =
  {
    'Hyundai': 105,
    'Tata': 120,
    'Mahindra': 98,
    'Renault': 113
  };
  const sortedDict: Record<string, number> = sortDictByValue(dict);
  console.log(sortedDict);

Output

{ Tata: 120, Renault: 113, Hyundai: 105, Mahindra: 98 }

Using Object.keys() and Array.sort()

This approach involves first converting the dictionary into an array of key-value pairs using Object.entries(). We then sort these pairs based on their corresponding values using Array.sort(). Finally, we reconstruct the sorted dictionary from the sorted array.

Example:

function sortDictByValue(dict: Record<string, number>): Record<string, number> {
    const entries = Object.entries(dict);
    const sortedEntries = entries.sort((a, b) => b[1] - a[1]);
    const sortedDict: Record<string, number> = {};
    sortedEntries.forEach(([key, value]) => {
        sortedDict[key] = value;
    });
    return sortedDict;
}

const dict: Record<string, number> = {
    'Hyundai': 105,
    'Tata': 120,
    'Mahindra': 98,
    'Renault': 113
};

const sortedDict: Record<string, number> = sortDictByValue(dict);
console.log(sortedDict);

Output

{ Tata: 120, Renault: 113, Hyundai: 105, Mahindra: 98 }
Article Tags :