在 JavaScript 中按 3 个不同的标准排序
Sorting by 3 different criteria in JavaScript
我正在尝试对下面的列表进行排序,首先按关系(如果它是订户,则它应该排在第一位)然后按活跃与不活跃。 Active 应该在 Inactive 之前,然后每个 active 和 inactive 都应该按年龄降序排序。
所以我有这个数组:
[{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
结果应该是:
[{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1992', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
我在下面尝试过,但似乎不起作用:
testlist = testlist.sort((a, b) => (a.Relationship === "Subscriber" ) ? -1 :(a.status === "Active") ? -2 : (b.BirthDate - a.BirthDate) ? -3 : 1);
有没有办法使用 JavaScript 中的 .sort 方法来做到这一点?
使用Array#sort
:
const arr = [ {status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"}, {status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"}, {status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"}, {status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"}, {status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"} ];
const sorted = arr.sort(
(
{ status: statusA, Relationship: relationshipA, BirthDate: birthDateA },
{ status: statusB, Relationship: relationshipB, BirthDate: birthDateB }
) => {
const relationship = (relationshipB === 'Subscriber') - (relationshipA === 'Subscriber');
if(relationship) return relationship;
const status = (statusB === 'Active') - (statusA === 'Active');
if(status) return status;
const birthday = (new Date(birthDateA)).getTime() - (new Date(birthDateB)).getTime();
return birthday;
});
console.log(sorted);
这是一种方法
let arr = [{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
arr.sort((a,b)=> (a.status.localeCompare(b.status) || new Date(a.BirthDate) - new Date(b.BirthDate)));
let index = arr.findIndex(i => i.Relationship === "Subscriber")
arr.unshift(arr.splice(index, 1)[0])
console.log(arr)
我正在尝试对下面的列表进行排序,首先按关系(如果它是订户,则它应该排在第一位)然后按活跃与不活跃。 Active 应该在 Inactive 之前,然后每个 active 和 inactive 都应该按年龄降序排序。 所以我有这个数组:
[{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
结果应该是:
[{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1992', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
我在下面尝试过,但似乎不起作用:
testlist = testlist.sort((a, b) => (a.Relationship === "Subscriber" ) ? -1 :(a.status === "Active") ? -2 : (b.BirthDate - a.BirthDate) ? -3 : 1);
有没有办法使用 JavaScript 中的 .sort 方法来做到这一点?
使用Array#sort
:
const arr = [ {status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"}, {status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"}, {status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"}, {status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"}, {status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"}, {status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"} ];
const sorted = arr.sort(
(
{ status: statusA, Relationship: relationshipA, BirthDate: birthDateA },
{ status: statusB, Relationship: relationshipB, BirthDate: birthDateB }
) => {
const relationship = (relationshipB === 'Subscriber') - (relationshipA === 'Subscriber');
if(relationship) return relationship;
const status = (statusB === 'Active') - (statusA === 'Active');
if(status) return status;
const birthday = (new Date(birthDateA)).getTime() - (new Date(birthDateB)).getTime();
return birthday;
});
console.log(sorted);
这是一种方法
let arr = [{status: 'Active', BirthDate: '12/31/1960', Relationship:"Friend"},
{status: 'Active', BirthDate: '12/31/1985', Relationship:"Wife"},
{status: 'Inactive', BirthDate: '12/31/1998', Relationship:"Son"},
{status: 'Inactive', BirthDate: '12/31/1996',Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1995', Relationship:"Daughter"},
{status: 'Active', BirthDate: '12/31/1979', Relationship:"Subscriber"},
{status: 'Inactive', BirthDate: '12/31/1999', Relationship:"Son"}
]
arr.sort((a,b)=> (a.status.localeCompare(b.status) || new Date(a.BirthDate) - new Date(b.BirthDate)));
let index = arr.findIndex(i => i.Relationship === "Subscriber")
arr.unshift(arr.splice(index, 1)[0])
console.log(arr)