Recently in the problem of JS sorting, because the database sort is too resource-intensive, if can be transferred to the client to sort, can greatly d reduce the server memory consumption. Client's words, in addition to JS, is as, unfortunately I learn too bad, so can only choose JS to study ... After my test, JS built-in method sort is very efficient
We know that the sort function is provided by default in JS, but this function is arranged by default in ascending order of the ASCII code of the array content, what if we want to sort the two-dimensional array? In PHP there is a multi_sort function to call, but there seems to be no such function in JS, but it doesn't matter, because the JS sort function also provides parameters can define the comparison function for the ordering of two-dimensional arrays.
1. Sort by value
Suppose you have the following array
Copy CodeThe code is as follows:
var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3];
Here, if we want to sort by the first column of each sub-array, we can define a comparison function:
Copy CodeThe code is as follows:
Arr.sort (function (x, y) {
return x[0]–y[0];
});
What is the function of the comparison here? In fact, the array elements are copied to x, Y, such as first arr[0] to x,arr[1] assigned to Y, and then x[0]–y[0], according to the returned value, if the return is greater than 0 of the number, then the array of x is placed after the Y, if the return of 0 is unchanged, Less than 0 puts X in front of y, then the first one is sorted so that the following two sorts are done until the entire array is sorted. This is the default ascending comparison function, if you want to sort in descending order just modify the comparison, instead return y[0]–x[0], here we x[0] to be sorted by the first column, we can also be sorted by other columns. The sort by default modifies the array structure of ARR, so that after sorting arr is an array of ascending columns in the first column.
2. Sort by string
If we sort by string, we can use the Localecompare method provided by JS,
Localecompare effect: Compares two strings in a local specific order.
The rule for using the Localecompare method is Stringobject.localecompare (target), and if Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is greater than target, the method returns a number greater than 0. If two strings are equal, or if there is no difference based on the local collation, the method returns 0, the comparison uses local rules, and the local rule means that the rules for sorting these local characters are sorted using the underlying operating system. By default, comparisons such as using the greater-than sign are only a two-character Unicode number, and are inconsistent with many languages.
Like what
Copy CodeThe code is as follows:
var arr = [[' Middle ', ' country '], [' Ah ', ' '], [' Oh ', ']];
Arr.sort (function (x, y) {
Return X[0].localecompare (y[0]);
});
The result will be in the first column of the text in alphabetical order, if it contains English, the default is to put English in front, if it is pure English, will be alphabetical, uppercase in the lower case, so you can achieve the sorting of strings, including Chinese and English mixed row. As for descending order, the method is the same as above, and changed to return Y[0].localecompare (X[0]); Can.
This sort of problem is realized, JS two-dimensional array sorting used to a lot of places. I hope we can help some people.
Copy CodeThe code is as follows:
function Tblsort (s) {
for (R = 0;r < row_len;r++) {
Arrs[r]=[]
for (c=0;c<cel_len;c++) {
ARRS[R][C] ={}//A two-dimensional array to create an object;
arrs[r][c].html = table.rows[r].cells[c].innerhtml//The table HTML is placed in an associative array, used for sorting and displaying on the page;
var text = table.rows[r].cells[c].innertext//Gets the text content of the table for the following judgment;
Detect possible content if it is not normal data becomes negative, ranked in the last side;
if (Text = = '-') {
arrs[r][c].text= '-1 ';
}else if (text== ' re-detection ') {
Arrs[r][c].text= '-2 ';
}else if (text== ' no baidu snapshot ' | | text== ' no rank data ') {
Arrs[r][c].text= '-3 '
}else if (Text = = ") {
Arrs[r][c].text= '-4 '
}else{
arrs[r][c].text=table.rows[r].cells[c].innertext//the contents of the table text into an associative array. Use the sort below to do the sorting.
}
}
}
Alert (arrs[0][0][' text ')
Reve judge whether the current is normal or inverse order;
if (Reve) {
Arrs.sort (function (x, y) {
return parsefloat (y[s][' text ")-parsefloat (x[s][' text '])
});
Reve=0
}else{
Arrs.sort (function (x, y) {
return parsefloat (x[s][' text '])-parsefloat (y[s][' text ")//Sort
})
Reve=1
}
Put the HTML content in the table
for (R = 0;r < row_len;r++) {
for (c=0;c<cel_len;c++) {
table.rows[r].cells[c].innerhtml =arrs[r][c][' html ']
}
}
}
End of Table sort
A summary of the usage of JS sort two-dimensional array sorting