我试图在下面排序这个列表,首先按关系(如果它是订阅者,那么它应该是第一个),然后按活跃与不活跃。活动应该在非活动之前,然后每个活动和非活动应该按年龄降序排序。我有这个数组:
[{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);
是否有一种方法来做到这一点,使用。sort方法在JavaScript?
使用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)