- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript sort(): Sort an Array
The JavaScript sort() method is used to sort an array. For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const myArray = ["Boston", "Austin", "Seattle", "Oakland", "Denver"]; myArray.sort(); document.getElementById("xyz").innerHTML = myArray; </script> </body> </html>
In the above example, elements of the array myArray are sorted in alphabetical order.
JavaScript sort() syntax
The syntax of the sort() method in JavaScript is:
array.sort(compareFun)
The array refers to the array whose elements are going to be sorted and compareFun is an optional parameter that is is used when we need to define the sort order using a self-defined function. This self-defined function returns any of the following three values:
- positive
- zero
- negative
These return values depend on the arguments given to the function (self-defined). For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const x = [30, 10, 100, 67, 23]; x.sort(function(m, n){return m-n}); document.getElementById("xyz").innerHTML = x; </script> </body> </html>
If we remove the compareFun parameter from the sort() method, used in the above example, in this way:
x.sort();
then the output should be:
It is because 23 is bigger than 100, and 2 is bigger than 1. Therefore, using the function, the return value of a-b, or, let's say, 23-100, gives a negative value, meaning that 23 comes before 100. Therefore, use the compareFun parameter to the sort() method only when sorting an array that contains numbers as its elements.
Sort an array in ascending order
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const arr = [12, 23, 43, 10, 435, 100, 122]; arr.sort(function(x, y){return x-y}); document.getElementById("xyz").innerHTML = arr; </script> </body> </html>
Sort an array in ascending order
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const a = [12, 23, 43, 10, 435, 100, 122]; a.sort(function(x, y){return y-x}); document.getElementById("xyz").innerHTML = a; </script> </body> </html>
Sort an array in alphabetical order
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> const myarr = ["Boston", "Austin", "Seattle", "Oakland", "Denver"]; myarr.sort(); document.getElementById("xyz").innerHTML = myarr; </script> </body> </html>
« Previous Tutorial Next Tutorial »