I have an array of objects which has null values also similar to the one below
let customerData = [{
id: 1,
name: 'cust1',
address: null
},
{
id: 2,
name: 'cust2',
address: 'test1'
},
{
id: 3,
name: 'cust3',
address: 'add2'
},
{
id: 4,
name: 'cust4',
address: 'test2'
},
{
id: 5,
name: 'cust5',
address: null
}
];
In asc/desc the null values are always sorted at the last.Can anyone please help me to sort the null values also?Either move all the null values to the top when asc/desc.
If we replace the null values by blank, the sorting works as expected.But we cannot change the data to empty as the null and empty values are treated differently.
I have seen many similar posts but all of them are making the null values to push below like the links below.
How to sort an array with null values
Please dont mark this question as duplicate as i couldn't find a proper solution.
The solutions i have tried
data.sort(function (item1, item2) {
------------------/////---not working------------------------------
if (item1[colBinding] === null && sort.direction === 'asc') return 1;
if (item2[colBinding] === null && sort.direction === 'asc') return 0;
if (item1[sort.colBinding] === null && sort.direction === 'desc') return 1; if (item2[sort.colBinding] === null && sort.direction === 'desc') return -1;
if (sort.direction === 'asc') return item1[sort.colBinding] > item2[sort.colBinding];
if (sort.direction === 'desc') return item1[sort.colBinding] < item2[sort.colBinding];
-------------------------------------------------------
----------------------//working to push null at the end
let currentData = item1[colBinding] === null ? '' : item1[sort.colBinding];
let nextData = item2[sort.colBinding] === null ? '' : item2[sort.colBinding];
if (currentData === nextData) { return -1 };
if (currentData < nextData) { return 0 };
if (currentData > nextData) { return 1 };
----------------------------------------------------------
---------------------/// not working-----------------------------------
var nullPosition = sort.direction === 'asc' ? 1 : -1;
if (item1[colBinding] === null) return nullPosition;
if (item2[colBinding] === null) return -nullPosition;
if (item1[colBinding] < item2[colBinding]) return -nullPosition;
if (item1[colBinding] > item2[colBinding]) return nullPosition;
return 0
------------------------------------------------------------------------------
});
You could check the value for null
first and sort them to top, then sort by the value.
var data = [{ id: 1, name: 'cust1', address: null }, { id: 2, name: 'cust2', address: 'test1' }, { id: 3, name: 'cust3', address: 'add2' }, { id: 4, name: 'cust4', address: 'test2' }, { id: 5, name: 'cust5', address: null }];
data.sort(function (a, b) {
return (b.address === null) - (a.address === null) || ('' + a.address).localeCompare(b.address);
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For descending sort, just reverse the second part
var data = [{ id: 1, name: 'cust1', address: null }, { id: 2, name: 'cust2', address: 'test1' }, { id: 3, name: 'cust3', address: 'add2' }, { id: 4, name: 'cust4', address: 'test2' }, { id: 5, name: 'cust5', address: null }];
data.sort(function (a, b) {
return (b.address === null) - (a.address === null) || ('' + b.address).localeCompare(a.address);
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments