Javascript Sorting - Example 4

Intermediate sorting

This page demostrates the use of a comparison function with the Javascript sort method. The function will perform a sort in decreasing order.

For more information return to the article "Sort Your Data with Javascript".

Example code

// Create a test array
var testArray = new Array(5, 4444, 333, 22222, 11);  
// Create a comparison function
function numberTest(a, b) {
return b - a;
}
// Sort all elements of the array using a comparison function
testArray.sort(numberTest); 

This table contains the original array and the sorted array.

# Original Array Sorted Array
1
2
3
4
5