# Js sort 2d array sort usage summary

• 2020-03-30 01:25:21
• OfStack

Recently in the js sort problem, because the database sort is too expensive resources, if you can move to the client to sort, can greatly reduce the server memory consumption. Client, in addition to js, is as, but I learn as too bad, so I can only choose js to study... In my tests, the js built-in method sort is very efficient

We know that sort is provided by default in js, but this function is arranged by default according to the ASCII ascending order of the array contents, so what do we do if we want to sort a two-dimensional array? There's a multi_sort function that you can call in PHP, but there doesn't seem to be a multi_sort function in js, but it doesn't matter, because js sort actually provides an argument that allows you to define a comparison function to sort a two-dimensional array.

1. Sort by value
Suppose I have the following array

``````
var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]];
``````

Here if we want to sort by the first column of each subarray, we can define a comparison function:
``````
arr.sort(function(x, y){
return x[0]  �  y[0];
});
``````

What does the comparison function do here? Is actually an array copying array element in turn to the x, y, such as the first will be arr [0] is assigned to x, arr [1] is assigned to y, then use x [0] � y [0], according to the value returned, if the return is greater than zero, then put in the array x y, if return is 0 is the same, put x in the front of the y less than zero, then the first sort good after in the following two sorting, until the entire array sorted. This is the default ascending comparison function. If you want to sort in descending order, you just need to change the comparison method to return y[0] and x[0]. Here, we say that x[0] is sorted by the first column. Sorting here by default changes the arr array structure, so arr is sorted in ascending order of the first column.

Sort by string
What about sorting by string, we can use the localeCompare method provided by js,
LocaleCompare does: compare two strings in a locally specific order.
LocaleCompare method using rules is stringObject localeCompare (target), if the stringObject is less than the target, then localeCompare () returns the number of less than 0. If stringObject is greater than target, the method returns a number greater than 0. If two strings are equal, or according to local sorting rules there is no difference, the method returns 0, more use of the rules of the local, local rules which means using the operating system the underlying rules of these local character sorting sorted, by default, such as the use of greater than such comparisons are simply compare two characters on the number of unicode size, would not in conformity with many languages.
Such as

``````
var arr = [[' In the ',' countries '], [' ah ',' the '], [' oh ',' the ']];
arr.sort(function(x, y){
return x[0].localeCompare(y[0]);
});
``````

The result is sorted by the pinyin of the text in the first column. If it contains English, the default is to put English first. If it is pure English, it will be sorted alphabetically, with uppercase after lowercase. For descending, return y[0]. LocaleCompare (x[0]); Can.

Related articles: