gpt4 book ai didi

javascript - 按优先于其中一个的两个值排序

转载 作者:IT王子 更新时间:2023-10-29 03:04:01 27 4
gpt4 key购买 nike

如何按 countyear 值按升序对数据进行排序,并优先考虑 count 值?

//sort this
var data = [
{ count: '12', year: '1956' },
{ count: '1', year: '1971' },
{ count: '33', year: '1989' },
{ count: '33', year: '1988' }
];
//to get this
var data = [
{ count: '1', year: '1971' },
{ count: '12', year: '1956' },
{ count: '33', year: '1988' },
{ count: '33', year: '1989' },
];

最佳答案

( See the jsfiddle )

var data = [
{ count: '12', year: '1956' },
{ count: '1', year: '1971' },
{ count: '33', year: '1989' },
{ count: '33', year: '1988' }
];

console.log(data.sort(function (x, y) {
var n = x.count - y.count;
if (n !== 0) {
return n;
}

return x.year - y.year;
}));

关于javascript - 按优先于其中一个的两个值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4576714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com