I am trying to sort this List below, first by Relationship(If it is subscriber then it should be first) then by Active vs inactive. Active should be before Inactive then each of the active and inactive should be sorted by descending age order. So I have this array:
[{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"}
]
and the result should be:
[{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"}
]
I tried this below but doesnt seem to work:
testlist = testlist.sort((a, b) => (a.Relationship === "Subscriber" ) ? -1 :(a.status === "Active") ? -2 : (b.BirthDate - a.BirthDate) ? -3 : 1);
Is there a way to do this by using the .sort method in JavaScript?
CodePudding user response:
Using 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);
CodePudding user response:
Here's one way to do it
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)