Thursday, September 4, 2025
HomeLanguagesJavascriptD3.js | d3.descending() Function

D3.js | d3.descending() Function

The d3.descending() function in D3.js is a comparator function for the reverse natural order and returns -1 if it takes two parameters in descending order, 1 if it takes two parameters in ascending order and 0 if it takes two equal parameters.

Syntax:

d3.descending(a, b)

Parameters: This function accepts parameters a, b which are any two value.

Return Value: It returns -1 if it takes two parameters in descending order (1st parameter is greater than 2nd parameter) or 1 if it takes two parameters in ascending order (2nd parameter is greater than 1st parameter) or 0 if it takes two equal parameters.

Below programs illustrate the d3.descending() function in D3.js.

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <title>d3.descending() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling to d3.descending() function with 
        // two integer value parameters
        A = d3.descending(50, 20);
        B = d3.descending(2, 5);
        C = d3.descending(5, 5);
           
        // Getting the results
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        console.log(A);
    </script>
</body>
  
</html>                    


Output:

-1
1
0

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <title>d3.descending() function</title>
  
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>
  
<body>
    <script>
          
        // Calling to d3.descending() function with 
        // some alphabetical parameters
        A = d3.descending();
        B = d3.descending("a", "b");
        C = d3.descending("b", "a");
        D = d3.descending("b", "b");
        E = d3.descending("b");
           
        // Getting the results
        document.write(A + "<br>");
        document.write(B + "<br>");
        document.write(C + "<br>");
        document.write(D + "<br>");
        document.write(E + "<br>");
    </script>
</body>
  
</html>                    


Output:

NaN
1
-1
0
NaN

NOTE: If this function takes alphabets as the parameter, it considers them in ascii form and evaluates the result accordingly and if the given parameter does not match the format of its parameter then it returns NaN i.e, Not a Number.

Reference: https://devdocs.io/d3~5/d3-array#descending

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6717 POSTS0 COMMENTS