0

私は次のような数字を並べ替えようとしています:

<1E-8

0.000027

0.000061

0.0018

0.0094

<8.64e-12

0.049

「<」は、真の値が指定された数値よりも小さいことを意味します。

これが私の「降下関数」であり、私はこれに非常に自信を持っています。

$.fn.dataTableExt.oSort['scientific-desc'] = function(a,b) {
                    var x = a.replace(/^[<>]/g,"");
                    var y = b.replace(/^[<>]/g,"");
                    x = parseFloat(x);
                    y = parseFloat(y);

                    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

そして、私は同様に「上昇関数」を定義しました:

$.fn.dataTableExt.oSort['scientific-asc'] = function(a,b) {
                    var x = a.replace(/^[<>]/g,"");
                    var y = b.replace(/^[<>]/g,"");
                    x = parseFloat(x);
                    y = parseFloat(y);

                    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

私は初期化コードのほぼすべてと上記の並べ替え関数で遊んだことがありますが、テーブルで正しく並べ替える数値を取得できるものはないようです。1E-8未満の数字は常に一緒にとどまる傾向があるため、小文字の「e」が付いている数字も一緒になります。

dataTableを初期化するコードは次のとおりです。これはAJAX呼び出しの内部で呼び出されるコードであることに注意してください。

$.get('xyz.json',
    function(data) {
        // deal with json data 
        // get it ready for dataTable
        // ... 

    $('.table').dataTable( {
                    "sScrollY": "200px",
                    "aoColumns": [
                        null,
                        null,
                        {"bSortable": true, "sType": "scientific"},
                        {"bSortable": false}
                    ],
                    "aaSorting": [ [2,'asc'] ],
                    "bPaginate": false,
                    "bFilter": false,
                    "iDisplayLength": 5,
                    "bRetrieve": true,
                    "bDestroy": true
    } );
});
4

3 に答える 3

0

あなたの例では、'<'記号の付いた数字、ソートされたリストで隣り合っています。

var A= ['<1E-8', 0.000027, 0.000061, 0.0018, 0.0094, '<8.64e-12', 0.049];

A.sort(function(a, b){
    var a1= String(a).replace(/^(<|>)/, ''),
    b1= String(b).replace(/^(<|>)/, '');
    return a1-b1;
}).join('\n');

<8.64e-12
<1E-8
0.000027
0.000061
0.0018
0.0094
0.049

//To have a decending sort, just reverse it-

 A.sort(function(a, b){
    var a1= String(a).replace(/^(<|>)/, ''),
    b1= String(b).replace(/^(<|>)/, '');
    return a1-b1;
}).reverse().join('\n');


0.049
0.0094
0.0018
0.000061
0.000027
<1E-8
<8.64e-12
于 2012-04-28T05:00:20.230 に答える
0

ソート機能に「高い自信」を持っているのは正しいことです。コンソールでaとbをすばやく印刷すると、並べ替え関数がhtmlエンティティを渡されていることがわかりました

&lt;

「<」ではありません。

別のstackoverflowスレッドに感謝します:

varTitle = $('<div />').html("Chris&apos; corner").text();
于 2012-05-05T16:59:55.163 に答える
0

DataTablesでカスタム並べ替えを行う別の方法は、テーブルセルに隠されたものを含めることです。

<tr>
  <td>Large<input type="hidden" value="3"></td>
</tr>
<tr>
  <td>Small<input type="hidden" value="1"></td>
</tr>
<tr>
  <td>Medium<input type="hidden" value="2"></td>
</tr>

次に、表示された値ではなく、非表示の値で並べ替えます。

// Tell DataTables to use this sort type always
$.fn.dataTableExt.aTypes.unshift(
   function () {
       return 'custom-sort';
   }
);


$.extend($.fn.dataTableExt.oSort, {
    // The selector
    "custom-sort-pre": function(a) {
        var sortValue = $(a).val();
        if (sortValue === undefined) {
            return a;
        }

        if (sortValue == 'NaN') {
            return NaN;
        }

        var floatValue = parseFloat(sortValue);
        if (isNaN(floatValue)) {
            return sortValue;
        }
        return floatValue;
    },

    // Asc sorting
   "custom-sort-asc": function (a, b) {
       if (isNaN(a) && !isNaN(b)) return 1;
       if (!isNaN(a) && isNaN(b)) return -1;
       if (isNaN(a) && isNaN(b)) return 0;

       if (a > b) return 1;
       if (a < b) return -1;
       return 0;
   },

    // Desc sorting
    "custom-sort-desc": function(a, b) {
        return $.fn.dataTableExt.oSort['custom-sort-asc'](a, b) * -1;
    }
});

この例は、文字列と数値の両方で機能します。

于 2012-10-03T10:15:28.537 に答える