Repository navigation
Expand file tree
/
Copy pathprogram23.js
More file actions
51 lines (35 loc) · 1.66 KB
/
Copy pathprogram23.js
File metadata and controls
51 lines (35 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//sorting program
// The sort() method sorts an array alphabetically:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
//The reverse() method reverses the elements in an array.
// You can use it to sort an array in descending order:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // First sort the elements of fruits
fruits.reverse(); // Then reverse the order of the elements
// You can fix this by providing a compare function:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function (a, b) {
console.log(a + " , " + b);
console.log(points);
return a - b;
});
// se the same trick to sort an array descending:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function (a, b) {
return b - a;
});
/*
The Compare Function
The purpose of the compare function is to define an alternative sort order.
The compare function should return a negative, zero, or positive value, depending on the arguments:
function(a, b){return a - b}
When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
If the result is negative a is sorted before b.
If the result is positive b is sorted before a.
If the result is 0 no changes are done with the sort order of the two values.
Example:
The compare function compares all the values in the array, two values at a time (a, b).
When comparing 40 and 100, the sort() method calls the compare function(40, 100).
The function calculates 40 - 100 (a - b), and since the result is negative (-60), the sort function will sort 40 as a value lower than 100.
*/