Javascript sorting of null values to be considered

Mothy

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
------------------------------------------------------------------------------

                });
Nina Scholz

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.

edited at
0

Comments

0 comments
Login to comment

Related

String sorting null values

Sorting NULL values in PostgreSQL

javascript sorting array of object according to date in string with some null values

Getting null by sorting map by values

MYSQL Null and empty values sorting

ModelState is always considered valid, regardless of null values in required fields

null values in Scenario Outline under Examples is considered as string in Karate

Javascript array sorting based on values

Javascript sorting input values into an array

Ignore null values when sorting by array in MongoDB

place null values at last while sorting the data

Incorrect sorting order of NULL values with a SQL view

Sorting on the earliest of 3 dates with null values

How to handle NULL values when sorting?

Remove all null array values before sorting

Would this implementation of sorting considered insertion sorting?

Javascript Filtering on Null Values

Getting unexpected result while sorting with null in JavaScript

Dealing with null values position when sorting an array based on numeric values

Sorting JavaScript Array Of Objects by two different values

Very strange javascript sorting values behaviour

sorting a javascript array of objects by 2 values

Sorting array values to object properties using Javascript

Javascript filter sorting by 2 or more values

custom sorting v-data-table with null values last

Custom sorting of null values in Lucene.Net 3.0.3

Sorting a DGV column ascending while putting null values at the bottom

SQL how to make null values come last when sorting ascending

Sorting date in string format with possible invalid/null values