Jssort two-dimensional array sorting usage summary _ javascript skills

Source: Internet
Author: User
We know that the sort function is provided by default in js, but this function is arranged in ascending order according to the ascii code of the array content by default, if we want to sort two-dimensional arrays, how can we solve the js sorting problem recently? Because database sorting is too resource-consuming, if it can be transferred to the client for sorting, can greatly reduce server memory consumption. In the client side, except js, it's just as. Unfortunately, I learned so badly that I can only choose js for research... After my tests, the built-in js method sort is very efficient.

We know that the sort function is provided by default in js, but this function is arranged in ascending order according to the ascii code of the array content by default. What should we do if we want to sort the two-dimensional array? In php, multi_sort functions are available for calling. However, this function does not seem to exist in js, but it does not matter, because the js sort function also provides parameters that can define comparison functions to sort two-dimensional arrays.

1. sort by value
Suppose there are the following Arrays:

The 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:

The Code is as follows:


Arr. sort (function (x, y ){
Return x [0]-y [0];
});


What is the role of the comparison function here? In fact, arrays copy array elements to x and y in sequence. For example, arr [0] is first assigned to x and arr [1] to y, then, use x [0]-y [0]. Based on the returned value, if the returned number is greater than 0, Put x in the array after y, if the return value is 0, it will not change. If the return value is less than 0, x will be placed in front of y. After the first one is sorted, the following two orders will be performed until the entire array is sorted. This is the default comparison function in ascending order. To sort data in descending order, you only need to change the comparison method to return y [0]-x [0, here we use x [0] To sort by the first column. Here we can also sort by other columns. By default, the array structure of arr is modified. After sorting, arr is an array in ascending order of the first column.

2. sort by string
To sort strings, we can use the localeCompare method provided by js,
LocaleCompare: Compares two strings in a specific local order.
The use rule of the localeCompare method is stringObject. localeCompare (target). If the stringObject is smaller than the target, localeCompare () returns a number smaller than 0. If the value of stringObject is greater than target, this method returns a value greater than 0. If the two strings are equal, or there is no difference according to the local sorting rules, this method returns 0, the comparison uses the local rule, A local rule means that the operating system is used to sort the rules for sorting local characters. By default, a comparison such as greater than a number only compares the unicode size of two characters, it is inconsistent with many languages.
For example

The Code is as follows:


Var arr = [['zhong', 'Guo '], [' Ah ','], ['Oh', '];
Arr. sort (function (x, y ){
Return x [0]. localeCompare (y [0]);
});


The results are sorted by the pinyin characters in the first column. If English is included, the English letters are placed first by default. If the English is pure, the results are sorted alphabetically, in this way, strings can be sorted in uppercase at the end of lower case, including Chinese and Chinese-English hybrid sorting. To sort data in descending order, change the method to return y [0]. localeCompare (x [0.

In this way, the Sorting Problem is realized, and there are still a lot of places to use js 2D array sorting. Hope you can help me.

The Code is as follows:


Function tblSort (s ){
For (r = 0; r <row_len; r ++ ){
Arrs [r] = []
For (c = 0; c Arrs [r] [c] ={} // create an object in a two-dimensional array;
Arrs?r=[c=.html = table. rows [r]. cells [c]. innerHTML // put the table HTML in an associated array for sorting and display on the page;
Var text = table. rows [r]. cells [c]. innerText // obtain the text of the table for the following judgment;
// Check whether the possible content becomes negative if it is not normal data, which is placed at the end;
If (text = '-'){
Arrs [r] [c]. text = '-1 ';
} Else if (text = 'recheck '){
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 // take the table text and put it in an associated array. Used for sorting the following sort.

}
}

}
// Alert (arrs [0] [0] ['text'])
// Reve determines whether the current sorting is normal or reverse;
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
}
// Add the prepared html content to the table
For (r = 0; r <row_len; r ++ ){
For (c = 0; c Table. rows [r]. cells [c]. innerHTML = arrs [r] [c] ['html']
}

}
}
// Table sorting ends

Related Article

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.