javascript 排序对象数组:字符串和数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 排序对象数组:字符串和数字相关的知识,希望对你有一定的参考价值。
let bySurname = [];
bySurname = clients.sort((a,b) => {
let x = a.Surname.toLowerCase();
let y = b.Surname.toLowerCase();
if (x > y) { return 1;}
if (x < y) { return -1;}
return 0;
});
console.log(bySurname);
console.log(clients);
const clients = [
{
Title: "Mrs",
FirstName: "Jo",
gender: "female",
Surname: "Bloggs",
Id: "1",
Address: {
Street: "60 Charlotte Street",
City: "London",
Postcode: "W1T 2NU"
}
},
{
Title: "Mrs",
FirstName: "Joan",
gender: "female",
Surname: "McKenzy",
Id: "2",
Address: {
Street: "1B Plaza Street",
City: "London",
Postcode: "W1T 2GD"
}
},
{
Title: "Mr",
FirstName: "Frankz",
gender: "male",
Surname: "Botanozi",
Id: "3",
Address: {
Street: "109 Saint Johns",
City: "London",
Postcode: "W1T 4TE"
}
},
];
-------------------------------------------------------------------
let cars = [
{make: 'toyota', year: 2016},
{make: 'honda', year: 2012},
{make: 'bmw', year: 1998},
{make: 'ford', year: 2002}
];
let carsByYear = [];
carsByYear = cars.sort((a,b) => {
return a.year - b.year;
});
console.log(carsByYear);
以上是关于javascript 排序对象数组:字符串和数字的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript数字数组怎么按数字大小排序?
JavaScript 对象数字键特性实现桶排序
JavaScript学习(六十六)—字符串对象常用的属性和方法总结以及数组元素的排序问题
JavaScript 数组对象数组排序
Javascript:自然排序的字母数字字符串
Javascript:自然排序的字母数字字符串