Recently, the problem of JS sorting is that the database sorting is too consuming resources. If it can be transferred to the client to sort, it can greatly reduce the server memory consumption. In the client, except JS, it is as AS. Unfortunately, I learned too badly, so I can only choose JS to study. Essence Essence After my test, the 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 according to the ASCII codes of the array content. What if we want to sort the two -dimensional array? There is a Multi_Sort function in PHP to call it, but there seems to be no such function in JS, but it does not matter, because the SORT function of JS also provides parameters that can define the comparison function for two -dimensional arrays.
1. Sort by numerical value
Assuming the following array
Copy code code as follows:
var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]];
What do we do if we want to sort the first column of each sub -array? We can define a comparison function:
Copy code code as follows:
Arr.sort (function (x, y) {
Return x [0] y [0];
});
What is the role of the comparison function here? In fact, the array is replicated to X, Y in turn, for example, first assign ARR [0] to X, ARR [1] to Y, and then use x [0] y [0]. According to the returned value, if the return value, if you return, return to the return. It is a number greater than 0, so put X in the array behind y. If 0 returns, it will not change, and the x will be placed in front of y, and then the first two are sorted. This sort is completed until the entire array sort is completed. This is a comparison function of the default sequence. If you want to arrange the order, you only need to modify the comparison method, and change it to Return y [0] x [0]. Here we x [0] indicates that it is sorted in the first column. Can also be sorted according to other columns. The sorting here will modify the array structure of the ARR, so the sorting ARR is an array that is promoted according to the first column.
2. Sort by string
If you are sorted by a string, we can use the Localecompare method provided by JS,
Localecompare: Compare two string in a particular order.
The use rules of the Localecompare method are StringObject.Localecompare (target). If StringObject is smaller than target, Localecompare () returns a number of less than 0. If StringObject is greater than target, the method returns a number greater than 0. If the two string are equal, or there is no difference according to the local sorting rules, this method returns 0. The device is compared with local rules. The local rules mean to sort the rules of these local characters with the bottom layer of the operating system. By default For example, the number of unicode that is purely compared with two characters is not consistent with many languages.
for example
Copy code code as follows:
var arr = [['in', 'country'], ['ah', ''], ['oh', '', '];
Arr.sort (function (x, y) {
Return x [0] .localecompare (y [0]);
});
As a result, it will be sorted in the pinyin of the first column of Chinese characters. If it contains English, the English is placed in front of the default. If it is pure English, it will be based on the letter. The sort of string is sorted, including Chinese and Sino -British mixed row. As for the order of ordering, the method is the same as the above, and the method is changed to Return y [0] .Localecompare (x [0]);
In this way, the problem of sorting is realized, and there are still many places for JS two -dimensional arrays. Hope to help some people
Copy code code as follows:
Function tblsort (s) {
for (r = 0; r <right_len; r ++) {
arRS [r] = []
for (c = 0; c <cel_len; c ++) {
ArRRS [R] [c] = {} // Create another object in the two -dimensional array;
Arrs [r] [c] .html = table.rows [r] .Cells [c] .innerhtml // Put the table of html into a associated array and displayed on the page after sorting;
var text = table.rows [r] .Cells [c] .innertext // Get the text content of the form for the following judgments;
// The detection that may appear to appear negative if the data is not normal, ranking at the end;
if (text == '-') {{
arRS [r] [c] .text = '-1';
} else if (text == 'reintegle') {{
arRS [r] [c] .text ='-2 ';
} else if (text == 'No Baidu snapshot' || Text == 'No ranking 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 // Put the text content of the table into a associated array. For sort below for sorting.
}
}
}
// Alert (ARRS [0] [0] ['Text'])
// Reve Judgment is currently sorted or desewess;
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']) //
})
Reve = 1
}
// Put the rowed HTML content in the form
for (r = 0; r <right_len; r ++) {
for (c = 0; c <cel_len; c ++) {
table.rows [r] .Cells [c] .innerhtml = Arrs [r] [c] ['html']]
}
}
}
// Table sorting ends