JavaScript Array sort() Method

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP







<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



JavaScript Array sort() Method


❮ JavaScript Array Reference



Example


Sort an array:



var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.sort();

<!--

The result of fruits will be:



Apple,Banana,Mango,Orange
-->
Try it Yourself »

More "Try it Yourself" examples below.



Definition and Usage


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 "Parameter
Values"
below).


Note: This method changes the original array.



Browser Support


The numbers in the table specify the first browser version that fully supports the method.














Method
sort() Yes Yes Yes Yes Yes

Syntax



array.sort(compareFunction)

Parameter Values





ParameterDescription
compareFunction
Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the
arguments, like:
  • function(a, b)return a-b

When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.


Example:


When comparing 40 and 100, the sort() method calls the compare
function(40,100).


The function calculates 40-100, and returns -60 (a negative value).


The sort function will sort 40 as a value lower than 100.




<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->




Technical Details






Return Value: The Array object, with the items sorted
JavaScript Version: ECMAScript 1

More Examples




Example


Sort numbers in an array in ascending order:



var points = [40, 100, 1, 5, 25, 10];

points.sort(function(a, b)return a-b);

<!--

The result of points will be:



1,5,10,25,40,100
-->
Try it Yourself »



Example


Sort numbers in an array in descending order:



var points = [40, 100, 1, 5, 25, 10];

points.sort(function(a, b)return b-a);

<!--

The result of points will be:



100,40,25,10,5,1
-->
Try it Yourself »



Example


Get the highest value in an array:



var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b)return b-a);    // Sort the numbers in the array in descending order
// The first item in the array (points[0]) is now the highest value

<!--

The result of points[0] will be:



100
-->
Try it Yourself »



Example


Get the lowest value in an array:



var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b)return a-b);    // Sort the numbers in the array in ascending order
// The first item in the array (points[0]) is now the lowest value

<!--

The result of points[0] will be:



1
-->
Try it Yourself »



Example


Sort an array alphabetically, and then reverse the order of the sorted items
(descending):



var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.sort();

fruits.reverse();

<!--

The result of fruits will be:



Orange,Mango,Banana,Apple
-->
Try it Yourself »



❮ JavaScript Array Reference

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ