简体   繁体   中英

javascript library that can sort table with scientific notation

I'm using the javascript class sorttable for my tables (see here: http://www.kryogenix.org/code/browser/sorttable/ )

However, I have a table that uses scientific notation. When I sort the column it looks like this...

9.43947E-20
9.41332E-22
9.36423E-22
9.35113E-18
9.2744E-23
9.18754E-22
9.09174
8.3981E-22
7.72743E-19
7.69538E-18
7.23656E-19

It is not properly sorted. I want it to look like this...

9.09174
9.35113E-18
7.69538E-18
7.72743E-19
7.23656E-19
9.43947E-20
9.41332E-22
9.36423E-22
8.3981E-22
9.2744E-23

Is there a javascript function that can do this? Thanks.

The above will work using my sorTable library (which happens to also use jQuery on the back currently):

Demo: http://jsfiddle.net/bBJBa/1/

They key is to convert the string value to numbers using something like *1 or parseFloat instead of parseInt .

var myArrayOfNumbersAsStrings.sort(function(a,b){
    if(parseFloat(a) < parseFloat(b)) return -1;
    if(parseFloat(a) > parseFloat(b)) return 1;
    return 0;
});

Try this:

numbers.sort(function(a, b) {
  return Number(a) - Number(b);
}); // => The array "numbers" is now sorted numerically ascending.

You want them sorted largest to smallest-

   var A= [
        '9.43947E-20',
        '9.41332E-22',
        '9.36423E-22',
        '9.35113E-18',
        '9.2744E-23',
        '9.18754E-22',
        '9.09174',
        '8.3981E-22',
        '7.72743E-19',
        '7.69538E-18',
        '7.23656E-19'
    ];
    A.sort(function(a, b){
        return b-a;
    }).join('\n')

    /*  returned value: (String)
    9.09174
    9.35113E-18
    7.69538E-18
    7.72743E-19
    7.23656E-19
    9.43947E-20
    9.41332E-22
    9.36423E-22
    9.18754E-22
    8.3981E-22
    9.2744E-23
    */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM