Home > Web Front-end > JS Tutorial > body text

Juery's method to solve tablesorter Chinese sorting and character range_jquery

WBOY
Release: 2016-05-16 16:00:36
Original
1507 people have browsed it

The example in this article describes Juery's method of solving tablesorter Chinese sorting and character range. Share it with everyone for your reference. The specific analysis is as follows:

tablesorter is an excellent table sorting plug-in among jQuery plug-ins. I believe everyone has used it or heard of it, so I won’t introduce it too much here. For more information, you can check out the official website: http://tablesorter .com/docs/ (the demo is relatively complete).

In several projects developed using tablesorter, two types of sorting problems were found, as follows:

The first problem is that Chinese characters cannot be sorted. This is because when sorting characters, the character size is compared using the unicode value. The code is as follows:

Js code

function sortText(a,b) {
  return ((a < b) &#63; -1 : ((a > b) &#63; 1 : 0));
};
function sortTextDesc(a,b) {
  return ((b < a) &#63; -1 : ((b > a) &#63; 1 : 0));
};
function sortText(a,b) {
  return ((a < b) &#63; -1 : ((a > b) &#63; 1 : 0));
};
function sortTextDesc(a,b) {
  return ((b < a) &#63; -1 : ((b > a) &#63; 1 : 0));
};
Copy after login

The result we want is to sort by Chinese pinyin, so we can modify the code to the following code:

Js code

function sortText(a,b) {
  return a.localeCompare(b);
};
function sortTextDesc(a,b) {
  return b.localeCompare(a);
};
function sortText(a,b) {
  return a.localeCompare(b);
};
function sortTextDesc(a,b) {
  return b.localeCompare(a);
};
Copy after login

The localeCompare method is a method that comes with JS. Needless to say, as the name implies, this method compares the size of characters based on the current region. However, this method cannot handle polyphonic characters.

The second problem is that numeric data that exceeds the range cannot be sorted. This is because the data value is distorted during numeric type conversion, for example:

Js code

alert(parseFloat('9999999999999999'));  // 10000000000000000
alert(parseFloat('10000000000000001')); // 10000000000000000
alert(parseFloat('10000000000000004')); // 10000000000000004
alert(parseFloat('10000000000000005')); // 10000000000000004
alert(parseFloat('10000000000000006')); // 10000000000000006
alert(parseFloat('9999999999999999'));  // 10000000000000000
alert(parseFloat('10000000000000001')); // 10000000000000000
alert(parseFloat('10000000000000004')); // 10000000000000004
alert(parseFloat('10000000000000005')); // 10000000000000004
alert(parseFloat('10000000000000006')); // 10000000000000006
Copy after login

Such a deviation will make the sorting results inaccurate. In order to avoid this problem, the original values ​​should not be used for comparison. Instead, weights should be introduced. From left to right, the weight corresponding to each numerical value decreases. Then the new value calculated based on the weight and the original value is used for comparison. This problem only needs to be modified by modifying the formatFloat method.

Js code

this.formatFloat = function(s) {    
  // TODO    
  var i = parseFloat(s);    
  return (isNaN(i)) &#63; 0 : i;    
};
Copy after login

I hope this article will be helpful to everyone’s jQuery programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!