# Numeric or Multi-dimensional Sorting
When you want to sort a simple Array it is not a difficult task, we just use the sort( )
method.
let arr = ["Charlie", "Aaron", "Michael", "Eric"];
let sortedArray = arr.sort();
2
When the array stores more complex data, like Objects or other Arrays, then sorting becomes a bit more complex. The other drawback to the standard sort function is that it sorts everything as if it were Strings only. It uses the String version of the numbers to sort them. This means that if you were to sort the numbers 1 to 12, then this is the result of the sort.
1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9
Likely not what you wanted.
Thankfully, the sort method allows us to provide our own custom sorting function.
# Sorting Numbers
To sort numbers we need to use the custom function and make it compare the numerical values.
The custom function will need to accept two parameters. The parameters that get passed in are the values from the array. The values to be compared will be continually be passed in until the array is sorted.
If compareFunction(a, b)
is less than 0, sort a to a lower index than b, i.e. a comes first.
If compareFunction(a, b)
returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
If compareFunction(a, b)
is greater than 0, sort b to a lower index than a.
compareFunction(a, b)
must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.
Here is the code version of these rules:
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
2
3
4
5
6
7
8
9
10
A simple easy way to create that function for comparing numbers is just to subtract one number from the other and return the result.
function compare(a, b) {
return a - b;
}
2
3
# Sorting Arrays with Javascript:
# Sorting Complex Objects
When it come to sorting more complex items like Objects or multi-dimensional (nested) arrays then we need to write a function that will look at those values and select the part or parts you are using to sort them.
Take this following example. We want to sort the array of students by their names. However, the array is made up of Objects, and the names are stored inside a name property inside each object. To do the sort we need to use the custom function and target the name property inside of each.
let students = [
{ name: "Alex", id: "040111222" },
{ name: "Devon", id: "040222333" },
{ name: "Riley", id: "040333444" },
{ name: "Cara", id: "040555666" },
{ name: "Cole", id: "040666777" },
{ name: "Bree", id: "040777888" }
];
students.sort(function(a, b) {
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Now the function will be able to use the return values to sort the students array.