This page demostrates the use of a comparison function with the Javascript sort method. The function will perform a sort in increasing order.
For more information return to the article "Sort Your Data with Javascript".
// Create a test array
var testArray = new Array(5, 4444, 333, 22222, 11);
// Create a comparison function
function numberTest(a, b) {
return a - b;
}
// 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 |