Lodash:排序对象数组,首先排列字母,然后是数字

时间:2016-12-27 02:55:47

标签: javascript arrays string sorting lodash

我有一系列对象,让我们说:

var objects = [
  {name: 'A'},
  {name: '1'},
  {name: 'B'}
]

知道我可以使用Lodash sortBy对其进行排序:

objects= _.sortBy(objects, 'name')

将导致此:

[
  {name: '1'},
  {name: 'A'},
  {name: 'B'}
]

但我想要的输出是:

[
  {name: 'A'},
  {name: 'B'},
  {name: '1'}
]

请帮忙。

5 个答案:

答案 0 :(得分:6)

使用Array#sort可以应用此逻辑:

// If both are numbers or both are not numbers
isNaN(a.name) === isNaN(b.name) ?
     // then compare between them 
    a.name.localeCompare(b.name)
    : // else
    // If the 1st is not a number move it up, if it's a number move it down
    (isNaN(a.name) ? -1 : 1); 

没有lodash:

var objects = [{"name":"A"},{"name":"3"},{"name":"1"},{"name":"B"}];

objects.sort(function(a, b) {
  return isNaN(a.name) === isNaN(b.name) ? a.name.localeCompare(b.name) : (isNaN(a.name) ? -1 : 1);
});

console.log(objects);

作为lodash连锁店的一部分:

var objects = [{"name":"A"},{"name":"3"},{"name":"1"},{"name":"B"}];

var result = _(objects)
  .sort(function(a, b) {
    return isNaN(a.name) === isNaN(b.name) ? a.name.localeCompare(b.name) : (isNaN(a.name) ? -1 : 1);
  }) 
  .value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.min.js"></script>

答案 1 :(得分:2)

我不确定使用lodash&#39; sortBy是否是解决此问题的正确方法。这是使用Javascript&#39; Array#sort方法的实现。

在进行排序时,不仅要考虑第一个字符,还要考虑整个字符串。

&#13;
&#13;
const objects = [{
  name: '2'
}, {
  name: 'B'
}, {
  name: '1'
}, {
  name: 'A'
}, {
  name: 'A1'
}, {
  name: 'AA'
}]

objects.sort((o1, o2) => {
  let a = o1.name, b = o2.name;
  let isDigit = x => x >= 48 && x <= 57;

  for (let i = 0, n = Math.min(a.length, b.length); i < n; i++) {
    let aCharCode = a.charCodeAt(i), bCharCode = b.charCodeAt(i);
    
    if (aCharCode !== bCharCode) {
      if (isDigit(aCharCode) && !isDigit(bCharCode)) return 1;
      if (isDigit(bCharCode) && !isDigit(aCharCode)) return -1;
      return aCharCode - bCharCode;
    }
  }

  return a.length - b.length;
});

console.log(objects)
&#13;
&#13;
&#13;

对于给定的输入,打印出

[
  {
    "name": "A"
  },
  {
    "name": "AA"
  },
  {
    "name": "A1"
  },
  {
    "name": "B"
  },
  {
    "name": "1"
  },
  {
    "name": "2"
  }
]

答案 2 :(得分:1)

有两个_.sortBy()

的解决方案

首先用于打字母字母,然后是另一个用于排序元素的字母。

在我看来,它更具可读性,不会对性能产生影响。

&#13;
&#13;
const objects = [
  {name: 'B'},
  {name: '2'},
  {name: '1'},
  {name: 'A'}
]

const result = _.sortBy(_.sortBy(objects, o => !isNaN(parseInt(o.name)), 'name'))

console.log(result)
&#13;
<script src="https://lodash.com/vendor/cdn.jsdelivr.net/lodash/4.17.3/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

如果name是一个整数前缀,则z用于比较。

var objects = [
  {name: 'z'},
  {name: 'A'},
  {name: '1'},
  {name: 'B'}
], sorted = _.sortBy( objects, [
        function(d) {
            return !isNaN(parseFloat(d.name))
                   && isFinite(d.name)
                       ? 'z' + d.name
                       : d.name;
        }
    ]
);
console.log(sorted);
<script src="https://lodash.com/vendor/cdn.jsdelivr.net/lodash/4.17.3/lodash.min.js"></script>

答案 4 :(得分:1)

对输入进行分区,单独排序并加入:

代码:

const _ = require('lodash');

const customSort = (a) => _.chain(_.partition(a, i => isNaN(i.name))).flatMap(p => _.sortBy(p, 'name')).value();

const input = [
    { name: '1' },
    { name: 'A' },
    { name: '6' },
    { name: 'B' },
    { name: 'a' },
    { name: '0' },
    { name: '3' }];

console.log(customSort(input));

输出:

[ { name: 'A' },
  { name: 'B' },
  { name: 'a' },
  { name: '0' },
  { name: '1' },
  { name: '3' },
  { name: '6' } ]