We know that the sort function is provided by default in JS, but this function is arranged by default in the ASCII order of the array contents, what if we want to sort the two-dimensional array?
Recently engaged in the problem of JS sorting, because the database ordering too much resources, if can be transferred to the client to sort, can greatly reduce server memory consumption. Client words, in addition to JS, is as, unfortunately I learn too bad, so can only choose JS to study ... 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 by default in the 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 does not matter, because the sort function of JS actually also provides parameters can define the comparison function of the order of two-dimensional arrays. 1, according to the numerical sort assumption has the following array code 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: The code is as follows: Arr.sort (function (x, y) { return x[0]–y[0];}); What is the function of the comparison function here? The array, in turn, copies the elements of the array to x,y, for example, first assign arr[0] to x,arr[1] and then use x[0]–y[0], depending on the value returned, if it returns a number greater than 0, then the x in the array is placed behind the Y, and if 0 is returned, Less than 0 puts X in front of y, then the first one is sorted after the following two, until the entire array sort completes. This is the default ascending comparison function, and if you want to sort in descending order, simply change the comparison and change to return y[0]–x[0], where we x[0 the order by the first column, where we can sort by other columns. The sorting here defaults to modifying the ARR array structure, so sorting the arr is an array in ascending order of the first column. 2, sorted by string, we can use the Localecompare method provided by JS, Localecompare function: To compare two strings in a local-specific order. The use rule for the Localecompare method is Stringobject.localecompare (target), and if Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is larger than tarGet, the method returns a number greater than 0. If two strings are equal, or if there is no difference based on local collations, the method returns 0, and the comparison uses local rules, which mean that the local rules are sorted by using the underlying rules of the operating system to sort the local characters. The comparison, by default, such as the greater-than number, is simply a comparison of the number of two-character Unicode, which is inconsistent with many languages. For example, the code is as follows: var arr = [['], ' country '], [' Ah ', ' '], [' Oh ', ']]; Arr.sort (function (x, y) { return X[0].localecompare (y[0));}; results will be sorted by the phonetic alphabet in the first column, if you have 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 sort of string, including Chinese and English mixed row. As to the descending order, the method is the same as above, changed to return Y[0].localecompare (X[0]); Can. This sort of problem is achieved, JS two-dimensional array ordering is still a lot of places. I hope I can help some people. Code is as follows: function Tblsort (s) { & nbsp for (R = 0;r < row_len;r++) { arrs[r]=[] &NB Sp for (c=0;c<cel_len;c++) { &NBS P ARRS[R][C] ={}//a two-dimensional array to create another object; arrs[r][c].html = table.rows[r].cells[c].innerhtml//The table HTML into an associative array for sorting and displaying on the page; var text = Table.rows[r].cells[c].inne rtext//Obtain the textual content of the form for the following judgments; //detection of possible content if the data is not normal to become negative, the end of the line; if (Text = = '-') { arrs[r][c].text= '-1 '; }else if (text== ' re-test ') { &NB Sp arrs[r][c].text= '-2 '; }else if (text== ' no baidu snapshot ' | | re>t== ' no rank data '] { ARRS[R][C]. Text= '-3 ' }else if (text = = ") { arrs[r][c].text= '-4 ' &N Bsp }else{ &NB Sp arrs[r][c].text=table.rows[r].cells[c].innertext//take form text into an associative array. Used for sorting below. } & nbsp { } //alert (arrs[0][0][' text ']) //reve to judge whether the current is a normal order or reverse sequence; if (Reve) { & nbsp Arrs.sort (function (x,y) { return parsefloat (y[s][' text '])-parsefloat (x[s][' text ']) &N Bsp }); reve=0 &NB Sp }else{ &NB Sp Arrs.sort (function (x,y) { return parsefloat (x[s][' text '])-parsefloat (y[s][' text ')/Sort {}) reve=1 & nbsp //placing the rows of HTML in the form for (R = 0;r < row_len;r++) { for (c=0;c< cel_len;c++) { &NBSP;TABL e.rows[r].cells[c].innerhtml =arrs[r][c][' html '] &NB Sp //table sort end