Monday, April 27, 2015

Sorted Array


Sorting a JavaScript array using array.sort()

The sort() method sorts the items of an array. 
The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down).
By default, the sort() method sorts the values as strings in alphabetical and ascending order.
This works well for strings ("Apple" comes before "Banana"). However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce an incorrect result when sorting numbers.
You can fix this by providing a "compare function" (See  below).

Syntax:
array.sort(compareFn)

Passing in a function reference into array.sort()

As touched on already, array.sort accepts an optional parameter in the form of a function reference (lets call it compareFn). The format of this function looks like this:

array.sort(CompareFn)

function CompareFn(a, b){
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
}

When such a function is passed into array.sort(), the array elements are sorted based on the relationship between each pair of elements "a" and "b" and the function's return value. The three possible return numbers are: <0 (less than 0), 0, or >0 (greater than 0):
  • Less than 0: Sort "a" to be a lower index than "b"
  •  Zero: "a" and "b" should be considered equal, and no sorting performed.
  •  Greater than 0: Sort "b" to be a lower index than "a".
To sort an array numerically and ascending for example, the body of your function would look like this:

function CompareFn(a, b){
return (a - b) //causes an array to be sorted numerically and ascending
}

Sorting an array in numerical order:

To sort an array in numerical order, simply pass a custom sortfunction into array.sort() that returns the difference between "a" and "b", the two parameters indirectly/ automatically fed into the function:

//Sort numerically and ascending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]


This works the way it does because whenever "a" is less than "b", a negative value is returned, which results in the smaller elements always appearing to the left of the larger ones, in other words, ascending.
Sort an array numerically but descending isn't much different, and just requires reversing the two operands "a" and "b":

//Sort numerically and descending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return b - a}) //Array now becomes [41, 25, 8, 71]

Shuffling (randomizing) the order of an array:

To randomize the order of the elements within an array, what we need is the body of our CompareFn to return a number that is randomly <00, or >0, irrespective to the relationship between "a" and "b". The below will do the trick:

//Randomize the order of the array:
var myarray=[25, 8, "George", "John"]
myarray.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled

All Examples: click here
Unknown Software Engineer

No comments:

Post a Comment