The usage summary of the order of the JS sort two-dimensional array _javascript tips

Source: Internet
Author: User
Tags array sort server memory

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, JS built-in method sort is very efficient

We know that the sort function is provided by default in JS, but this function is sorted by default in the ASCII order of the array contents, 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, sorted by numerical value
Suppose you have the following array

Copy Code code as follows:

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

Here, if we're going to sort by 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 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
Sorted by string, we can use the Localecompare method provided by JS,
Localecompare function: Compares 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 the 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 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.
Like what

Copy Code code as follows:

var arr = [['], ' country '], [' Ah ', ' '], [' Oh ', ']];
Arr.sort (function (x, y) {
Return X[0].localecompare (y[0]);
});

The result is sorted by the phonetic alphabet of the text 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.

Copy Code code as follows:

function Tblsort (s) {
for (R = 0;r < row_len;r++) {
Arrs[r]=[]
for (c=0;c<cel_len;c++) {
ARRS[R][C] Create an object in a two-dimensional array of ={}//;
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].innertext//Gets the text content of the table for the following judgment;
Detect possible content if the data is not normal to become a negative number, 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. Used for sorting below.

}
}

}
Alert (arrs[0][0][' text '))
Reve whether the current is a normal or reverse 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 rows of HTML into a 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 '
}

}
}
Table Sort End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.