Javascript Sorting - Example 5

Intermediate sorting

This page demostrates the use of a comparison function with the Javascript sort method. The function will perform a reverse alphabetic sort.

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

Example code

// Create a test array
var wordList = new Array( "tiger", "cat", "dog");
// Create a comparison function
function reverseAlphabeticTest(a, b) {
   if (a < b) return 1;
   if (a > b) return -1;
   return 0;
// Sort all elements of the array using a comparison function

This table contains the original array and the sorted array.

# Original Array Sorted Array