jonschlinkert/array-sort

comparison to null strings

jrhii opened this issue · 4 comments

jrhii commented

It seems that if you are comparing strings in objects that may include null if won't sort properly.

jrhii commented

running this

`const nullArray = new Array();

while (array.some(obj => obj[element] === null)) {
    array.forEach((obj, index) => {
        if (obj[element] === null) {
            array.splice(index, 1);
            nullArray.push(obj);
        }
    });
}`

and then concat nullArray to the end of my return fixes it for what I need it to do.

A more detailed code example would help, so we can reproduce. Like examples of what's in the arrays.

@jonschlinkert here is example:

const test = [
  { title: 'b' },
  { title: null },
  { title: 'a' }
]

console.log(arraySort(test, 'title'))

Current output:

[
  { title: 'a' },
  { title: null },
  { title: 'b' }
]
doowb commented

Thanks @vladshcherbin for the example. A new version with logic for handling null and undefined has been published.