简体   繁体   English

在 firstName 和 lastName 之后对数组进行排序,javascript

[英]Sorting array after firstName and lastName, javascript

Currently I am sorting an array on firstName.目前我正在对 firstName 上的数组进行排序。

currentUsers = currentUsers.sort(function(a, b) {
    if (a.firstName > b.firstName) {
        return 1;
    } else if (a.firstName < b.firstName) {
        return -1;
    }
};

But I would like to have the lastName sorted as well.但我也想对 lastName 进行排序。 So that Bob Anderson would be sorted above Bob Bobson.这样 Bob Anderson 就会排在 Bob Bobson 之上。 I looked at another question here on stackoverflow which suggested that I should add:我在这里查看了另一个关于 stackoverflow 的问题,它建议我应该添加:

currentUsers = currentUsers.sort(function(a, b) {
    if (a.firstName > b.firstName) {
        return 1;
    } else if (a.firstName < b.firstName) {
        return -1;
    }

    if (a.lastName > b.lastName) {
        return 1;
    } else if (a.lastName < b.lastName) {
        return -1;
    } else {
        return 0;
    }
};

//Edit The problem I have is that the sorting keeps sorting on every letter in firstName. //编辑 我遇到的问题是排序一直对 firstName 中的每个字母进行排序。 How would I only sort on the first letter of firstName and then move on to the lastName?我如何只对 firstName 的第一个字母进行排序,然后继续对 lastName 进行排序?

Just concatenate them with an underscore只需用下划线连接它们

 var currentUsers = [{ firstName: "Bob", lastName: "Adams" }, { firstName: "Barney", lastName: "Jones" }, { firstName: "Freddie", lastName: "Crougar" }, { firstName: "Bobby", lastName: "Anderson" }, { firstName: "Joe", lastName: "Lewis" }, { firstName: "Joseph", lastName: "Lewis" }]; currentUsers = currentUsers.sort(sortOnFirstAndLast) function sortOnFirstAndLast(a,b) { var aa = a.firstName + ", " + a.lastName, bb = b.firstName + ", " + b.lastName; if (aa > bb) return 1; else if (aa < bb) return -1; return 0; } var $r = $("#result"); for (var i in currentUsers) { var div = $("<div/>").html(i + ":" + currentUsers[i].firstName + ":" + currentUsers[i].lastName); $r.append(div) }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result"></div>

You can try this ES6 version你可以试试这个 ES6 版本

 const currentUsers = [{ firstName: "Bob", lastName: "Adler" }, { firstName: "Barney", lastName: "Jones" }, { firstName: "Freddie", lastName: "Crougar" }, { firstName: "Bob", lastName: "Adams" }, { firstName: "Joe", lastName: "Lewis" }, { firstName: "Joseph", lastName: "Lewis" }]; const sortedUsers = currentUsers.sort((a, b) => { const result = a.firstName.localeCompare(b.firstName); return result !== 0 ? result : a.lastName.localeCompare(b.lastName); }) console.log(sortedUsers);

Try this;尝试这个;

 var currentUsers = [ { firstName: "Bob", lastName: "Bobson" },{ firstName: "Bob", lastName: "Anderson" }, { firstName: "Amy", lastName: "Jackson" }]; var sorted = currentUsers.sort(function(a, b) { var aFirstChar = a.firstName.charAt(0); var bFirstChar = b.firstName.charAt(0); if (aFirstChar > bFirstChar) { return 1; } else if (aFirstChar < bFirstChar) { return -1; } else { var aLastChar = a.lastName.charAt(0); var bLastChar = b.lastName.charAt(0); if (aLastChar > bLastChar) { return 1; } else if (aLastChar < bLastChar) { return -1; } else { return 0; } } }); alert(JSON.stringify(sorted));

Just for the sake of alternatives.只是为了替代品。 If you're only comparing the first char, you could use charCodeAt instead of charAt to get a numeric value for another way of comparing:如果您只比较第一个字符,则可以使用charCodeAt而不是 charAt 来获取用于另一种比较方式的数值:

currentUsers  = currentUsers.sort(function(a, b) {
    return a.firstName.charCodeAt(0) - b.firstName.charCodeAt(0) 
        || a.lastName.charCodeAt(0) - b.lastName.charCodeAt(0);
});

The || || executes only if the firstname first chars are equal (diff = 0)仅当 firstname first chars 相等(diff = 0)时才执行

The best, shortest and most readable code I could figure out was我能想到的最好、最短和最易读的代码是

currentUsers = currentUsers.sort((a, b) =>
    a.firstName.localeCompare(b.firstName) ||
    a.lastName.localeCompare(b.lastName)
);

'||' '||' allows the other comparison only execute when first localeCompare returns 0, aka.允许其他比较仅在第一个 localeCompare 返回 0 时执行,也就是。 both names being equal value.两个名字是相等的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM