使用lodash按数组列表中的字母排序

时间:2018-04-30 10:07:17

标签: javascript typescript lodash

如何在javascript中按字母顺序对数组列表中的名称进行排序,我尝试使用此代码

const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
]

sample.sort(sortBy('name', false, function(a){return a.toUpperCase()}));

但是在这个代码中它没有正常工作sortBy我正在使用lodash。如果有可能在lodash它会很好

5 个答案:

答案 0 :(得分:3)

<强>样本

VSProject
const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
];


var chars =_.orderBy(sample, [user => user.name.toLowerCase()], ['asc']);
 
console.log(chars);

答案 1 :(得分:1)

According to the lodash docs sortBy的API是:

_.sortBy(collection, [iteratees=[_.identity]])

在您的情况下,collection是您的sample数组,而您的[iteratees=[_.identity]]应该是一个函数或数组,它返回您要排序的键。

所以这可能就是你想要的:

_.sortBy(sample, ['name']);

OR

_.sortBy(sample, function(o){return o.name;}]);

答案 2 :(得分:1)

_.sortBy()

您的代码应该是:

<强>样本

const sample = [{
  name: "AddMain",
  mesg: "test000"
}, {
  name: "Ballside",
  mesg: "test004545"
}, {
  name: "TestMain",
  mesg: "test00"
}, {
  name: "ball",
  mesg: "test004545"
}, {
  name: "Main",
  mesg: "test004545"
}, {
  name: "alliswell",
  mesg: "test004545"
}];

let result = _.sortBy(sample, ({name}) => name.toLowerCase());

console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}
<script src="//lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

答案 3 :(得分:1)

const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
]

var result = sample.slice().sort((a,b) => a.name.localeCompare(b.name, undefined, {numeric: true}));
console.log(result);

答案 4 :(得分:1)

const characters = [
  { name: 'Jean-Luc Picard', age: 59 },
  { name: 'William Riker', age: 29 },
  { name: 'Deanna Troi', age: 28 },
  { name: 'Worf', age: 24 }
];

// Sort characters by the `age` property
const sorted = _.sortBy(characters, 'age');

console.log(sorted);

sorted[0].name; // Worf
sorted[1].name; // Deanna Troi
sorted[2].name; // William Riker
sorted[3].name; // Jean-Luc Picard
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js"></script>