在Javascript中按名字(按字母顺序)对数组进行排序

本文翻译自:Sort array by firstname (alphabetically) in Javascript

I got an array (see below for one object in the array) that I need to sort by firstname using JavaScript. 我有一个数组(请参阅下面的数组中的一个对象),我需要使用JavaScript对firstname进行排序。 How can I do it? 我该怎么做?

var user = {
   bio: null,
   email:  "user@domain.com",
   firstname: "Anna",
   id: 318,
   lastAvatar: null,
   lastMessage: null,
   lastname: "Nickson",
   nickname: "anny"
};

#1楼

参考:https://stackoom.com/question/SA6c/在Javascript中按名字-按字母顺序-对数组进行排序


#2楼

If compared strings contain unicode characters you can use localeCompare function of String class like the following: 如果比较字符串包含unicode字符,则可以使用String类的localeCompare函数 ,如下所示:

users.sort(function(a,b){
    return a.firstname.localeCompare(b.firstname);
})

#3楼

underscorejs offers the very nice _.sortBy function: underscorejs提供了非常好的_.sortBy函数:

_.sortBy([{a:1},{a:3},{a:2}], "a")

or you can use a custom sort function: 或者您可以使用自定义排序功能:

_.sortBy([{a:"b"},{a:"c"},{a:"a"}], function(i) {return i.a.toLowerCase()})

#4楼

A more compact notation: 更紧凑的符号:

user.sort(function(a, b){
    return a.firstname === b.firstname ? 0 : a.firstname < b.firstname ? -1 : 1;
})

#5楼

You can use the in-built array method - sort . 您可以使用内置数组方法 - sort This method takes a callback method as a param 此方法将回调方法作为参数



    // custom sort function to be passed as param/callback to the Array's sort method
    function myCustomSort(a, b) {
        return (a.toLowerCase() > b.toLowerCase()) ? 1 : -1;
    }

    // Actual method to be called by entity that needs sorting feature
    function sortStrings() {
        var op = Array.prototype.sort.call(arguments, myCustomSort);
    }

    // Testing the implementation
    var sortedArray = sortStrings("Burger", "Mayo1", "Pizza", "boxes", "Apples", "Mayo");
    console.log(sortedArray); //["Apples", "boxes", "Burger", "Mayo", "Mayo1", "Pizza"]

Key Points to be noted for understanding this code. 理解此代码需要注意的要点。

  1. The custom method, in this case, myCustomSort , should return +1 or -1 for each element pair(from the input array) comparison. 自定义方法(在本例中为myCustomSort )应为每个元素对(来自输入数组)的比较返回+1或-1。
  2. Use toLowerCase() / toUpperCase() in the custom sorting callback method so that case difference does not affect the correctness of the sorting process. 在自定义排序回调方法中使用toLowerCase() / toUpperCase() ,以便大小写差异不会影响排序过程的正确性。

I hope this is clear enough explanation. 我希望这是足够明确的解释。 Feel free to comment if you think, more info is needed. 如果您认为,请随时发表评论,需要更多信息。

Cheers! 干杯!


#6楼

You can use something similar, to get rid of case sensitive 您可以使用类似的东西来摆脱区分大小写

users.sort(function(a, b){

  //compare two values
  if(a.firstname.toLowerCase() < b.firstname.toLowerCase()) return -1;
  if(a.firstname.toLowerCase() > b.firstname.toLowerCase()) return 1;
  return 0;

})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值