D3.js | d3.descending() Function
Last Updated :
28 Jun, 2019
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>
</head>
<body>
<script>
A = d3.descending(50, 20);
B = d3.descending(2, 5);
C = d3.descending(5, 5);
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>
</head>
<body>
<script>
A = d3.descending();
B = d3.descending( "a" , "b" );
C = d3.descending( "b" , "a" );
D = d3.descending( "b" , "b" );
E = d3.descending( "b" );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...