Methods of Juery to solve tablesorter Chinese sorting and Character range

  • 2020-06-07 04:01:16
  • OfStack

An example of Juery is presented in this paper to solve the problem of tablesorter Chinese sorting and character range. Share to everybody for everybody reference. The specific analysis is as follows:

tablesorter is a relatively good jQuery plug-in 1 table sorting plug-in, I believe that you have used or heard, I here but is introduced, and more detailed information can see the official website: http: / / tablesorter com docs/demo done relatively complete ().

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

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

Js code


function sortText(a,b) {
  return ((a < b) ? -1 : ((a > b) ? 1 : 0));
};
function sortTextDesc(a,b) {
  return ((b < a) ? -1 : ((b > a) ? 1 : 0));
};
function sortText(a,b) {
  return ((a < b) ? -1 : ((a > b) ? 1 : 0));
};
function sortTextDesc(a,b) {
  return ((b < a) ? -1 : ((b > a) ? 1 : 0));
};

The result we want is to sort the characters in alphabetical order, 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);
};

The localeCompare method is the built-in method of JS. It is easy to understand that this method compares the size of characters under the current region, but it cannot handle polyphonics.

The second problem is that it is impossible to sort numeric data that is out of range because of the distortion of data values during numeric type conversion, such as:

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

Such deviation will make sorting result is not accurate, in order to avoid this kind of problem, should not use the original value, but should be introduced weights, numerical from left to right, each one numerical corresponding weight decreasing, and then according to the weight and the new value of the original values calculated for comparison, it only need to modify the formatFloat method can solve the problem.

Js code


this.formatFloat = function(s) {    
  // TODO    
  var i = parseFloat(s);    
  return (isNaN(i)) ? 0 : i;    
};

I hope this article has been helpful for your jQuery programming.


Related articles: