Javascript sorting of null values to be considered -


i have array of objects has null values similar 1 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 null values sorted @ last.can please me sort null values also?either move null values top when asc/desc.

if replace null values blank, sorting works expected.but cannot change data empty null , empty values treated differently.

i have seen many similar posts of them making null values push below links below.

how sort array null values

please dont mark question duplicate couldn't find proper solution.

the solutions 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 push null @ 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 check value null first , sort them top, sort 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, reverse 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; }


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -