Open In App

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 javascript constructors are expressed as functions and utilized in the same way. under collections come arrays, sets, and maps. Let’s learn how to sort them in this article.



We use the JavaScript sort() method to sort collections. This method sorts the array in place.

Syntax:



Array.prototype.sort()

SORTING AN ARRAY

Example 1: Sorting an array when the array has numbers as elements:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
        // Sorting an array in ascending order
        let array = [10, 2, 5, 12, 7];
        array = array.sort(function (a, b) {
            return a - b;
        });
        console.log("sorted array : " + array);
    </script>
</body>
  
</html>

Output:

Example 2: Sorting an array when array consists of strings:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
        // Sorting an array of strings
        let array = ["c", "b", "a"];
        array = array.sort();
        console.log("sorted array : " + array);
  
    </script>
</body>
  
</html>

Output:

SORTING A MAP

Sorting a map when:

Example 1: Sorting according to values:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
  
        // Creating a map and sorting it according to values.
        let newMap = new Map();
        newMap.set("a", 50);
        newMap.set("c", 40);
        newMap.set("b", 30);
        newMap.set("d", 10);
        newMap = Array.from(newMap).sort((a, b) => a[1] - b[1]);
        console.log(newMap);
    </script>
</body>
  
</html>

Output: 

Example 2: Sorting according to keys:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
        // Sorting map by keys
        let newMap = new Map();
        newMap.set("a", 50);
        newMap.set("c", 40);
        newMap.set("b", 30);
        newMap.set("d", 10);
        newMap = Array.from(newMap.entries()).sort();
        console.log(newMap);
    </script>
</body>
  
</html>

Output:

SORTING A SET

Sorting a set when:

Example 1: Sorting a set that contains numbers:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
        // Converting a set into an array and sorting it.
        new_set = Array.from(new Set([9, 9, 2, 4, 5, 11]))
          .sort(function (a, b) {
            return a - b;
        });
        console.log(new_set);
    </script>
</body>
  
</html>

Output:

Example 2: Sorting a set of strings:




<!DOCTYPE html>
<html>
  
<head>
    <title>Javascript sorting collections</title>
</head>
  
<body>
    <script>
        new_set = Array.from(new Set(["rachel", "sam", "daniel"]))
          .sort();
        console.log(new_set);
    </script>
</body>
  
</html>

Output:


Article Tags :