A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/Array/sort below:

sort · WebPlatform Docs

sort Summary

Sorts an Array.

Syntax
sort( sortFunction )
sortFunction
Optional. The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
Return Value

The sorted array.

Examples

The following example shows how to use the sort method.

var a = new Array(4, 11, 2, 10, 3, 1);

 var b = a.sort();
 document.write(b);
 document.write("<br/>");

 
 

 
 b = a.sort(CompareForSort);
 document.write(b);
 document.write("<br/>");
 

 
 function CompareForSort(first, second)
 {
     if (first == second)
         return 0;
     if (first < second)
         return -1;
     else
         return 1;
 }
Remarks

The sort method sorts the Array object in place; no new Array object is created during execution.

If you supply a function in the sortFunction argument, it must return one of the following values:

See also Specification

15.4.4.11 Array.prototype.sort (comparefn) ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011

Attributions

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4