skip to Main Content

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


  1. You can just add to sorting that the one with the "false" (if the other is true) is always "lower"

    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
    }]
    
    const sortedRows = (rows) => {
                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 (a?.isActive === false && b?.isActive === true) {
                      return 1;
                    }
    
                    if (a?.isActive === true && b?.isActive === false) {
                      return -1;
                    }
    
    
                    if (order === 'asc') {
                        return aData > bData ? 1 : -1;
                    }
                    return bData > aData ? 1 : -1;
                });
    };
    
    const sorted = sortedRows(arr);
    console.log(sorted);
    Login or Signup to reply.
  2. You could sort by boolean first and the by firstName with a default in a single function.

    const
        array = [{ 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 }];
    
    
    array.sort((a, b) => b.isActive - a.isActive || (a.firstName || '').localeCompare(b.firstName || ''));
    
    console.log(array);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search