javascript natural sort

孤街浪徒 提交于 2019-11-28 11:47:25

Try like this:

arr = arr.sort(function(a, b) {
  return +/\d+/.exec(a)[0] - +/\d+/.exec(b)[0];
});

Edit: Fixed it works now, it had a couple errors: http://jsbin.com/iwejik/1/edit

columnArray.sort(function(a,b) {
     return parseInt(a.match(/\d+/)[0],10) - parseInt(b.match(/\d+/)[0],10);
});

demo

There is a npm package for this : https://github.com/Bill4Time/javascript-natural-sort.Works quite fine on many scenarios.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!