I have an array that contain
var arr = [{
firstName: 'Jane',
lastName:'Doe',
age:19,
isActive: true
},
{
firstName: 'Wane',
lastName:'Doe',
age:21,
isActive: false
},
{
firstName: 'Kane',
lastName:'Doe',
age:23,
isActive: false
},
{
firstName: 'Mane',
lastName:'Doe',
age:25,
isActive: true
}]
The sorting function looks like
const sortedRows = (rows: any) => {
let order === 'asc'; // this is static for the time being
return [...rows].sort((a, b) => {
const aData = a['firstName'] === null ? '' : a['firstName'];
const bData = b['firstName'] === null ? '' : b['firstName'];
if (order === 'asc') {
return aData > bData ? 1 : -1;
}
return bData > aData ? 1 : -1;
});
};
The sorting works as expected but I would like to align isActive:false
elements at the end of the sorted rows.
So the final result should be like
[
{
"firstName": "Jane",
"lastName": "Doe",
"age": 19,
"isActive": true
},
{
"firstName": "Mane",
"lastName": "Doe",
"age": 25,
"isActive": true
},
// listing all isActive:false at the end
{
"firstName": "Kane",
"lastName": "Doe",
"age": 23,
"isActive": false
},
{
"firstName": "Wane",
"lastName": "Doe",
"age": 21,
"isActive": false
}
]
2
Answers
You can just add to sorting that the one with the "false" (if the other is true) is always "lower"
You could sort by boolean first and the by
firstName
with a default in a single function.