Friday, September 19, 2025
HomeLanguagesJavascriptD3.js nest.sortKeys() Function

D3.js nest.sortKeys() Function

D3.js is a library built with JavaScript and particularly used for data visualization. The nest.sortKeys() function is used to sort the keys in a particular order i.e. ascending or descending.

Syntax:

nest.sortkeys( comparatorFunction )

Parameters: This function accepts a single parameter comparatorFunction that is used to specify how to sort the keys. If no comparatorFunction is specified for the current key, the order in which keys will be returned is not defined.

Return Value: It returns the sorted key (ascending/descending) according to comparator function.

Below programs illustrate the nest.sortKeys() function in D3.js.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
  
    <title>
        D3.js nest.sortKeys() Function
    </title>
</head>
  
<body>
    <script>
          
        // Dataset
        var cars = [
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1952", model: "s51" },
            { name: "car1", manufactured: "1951", model: "s50" },
            { name: "car1", manufactured: "1953", model: "s52" },
            { name: "car1", manufactured: "1953", model: "s50" },
            { name: "car1", manufactured: "1950", model: "s52" },
        ];
  
        var groupedData = d3.nest()
            .key(function (d) { return d.manufactured; })
  
            // sorting keys in ascending order
            .sortKeys(d3.ascending)
            .entries(cars);
        console.log("ArrayData :", groupedData);
    </script>
</body>
  
</html>


Output:

keys sorted in ascending order

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
  
    <title>
        D3.js nest.sortKeys() Function
    </title>
</head>
  
<body>
    <script>
        var cars = [
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1952", model: "s51" },
            { name: "car1", manufactured: "1951", model: "s50" },
            { name: "car1", manufactured: "1953", model: "s52" },
            { name: "car1", manufactured: "1953", model: "s50" },
            { name: "car1", manufactured: "1950", model: "s52" },
        ];
        var groupedData = d3.nest()
            .key(function (d) { return d.manufactured; })
  
            // Sorting keys in descending order
            .sortKeys(d3.descending)
            .entries(cars);
        console.log("ArrayData :", groupedData);
    </script>
</body>
  
</html>


Output:

keys sorted in descending order

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS