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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

-1
1
0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.