2015-05-29 86 views
0

我想用逗號對一列數字進行排序,例如1,092,021。排序處理,作爲一個小數和分類不當。例如:排序包含逗號的數字

1,330,000 
2,350,000 
3 
5 
7,000,000 
etc 

有沒有辦法讓逗號分隔的數字排序正確?

+0

快速解決方法是刪除逗號,然後排序,然後將它們添加回。 – JaredT

回答

1

這裏是@JaredT提出的解決方案:

var nums = ['1,330,000', '2,350,000', '3', '5', '7,000,000', '1,000', '100']; 

nums 
    .map(function (n) { return parseInt(n.replace(/,/g, ''));}) 
    .sort(function (a, b) { return a > b;}) 
    .map(function (i) { 
     return String(i).split('') 
      .reverse() 
      .map(function (n, i) { 
       return n + (!i || (i % 3) ? '' : ','); 
      }) 
      .reverse() 
      .join(''); 
    }); 
+0

完美,感謝@Magomogo。添加此分揀機功能到柱: '分揀機:功能逗號(A,B){' \t \t \t \t \t \t'一個= parseInt函數(a.replace(/,/克, ''));' \t \t \t \t \t \t'b = parseInt函數(b.replace(/,/克, ''));' \t \t \t \t \t \t'如果(A B)返回-1;' \t \t \t \t \t'返回0;' \t \t \t \t'},' –

1

我也一樣@維尼 - 詹姆斯和它工作正常。我分享我的代碼。

'columns': [{ 
      field:'Checkbox' 
      },{ 
      field:'Status' 
      },{ 
      field:'Price', 
      visible:true, 
      sorter: function commas(a,b){ a = parseInt(a.replace(/,/g, '')); b = parseInt(b.replace(/,/g, '')); if (a < b) return 1; if (a > b) return -1; return 0; }]