JavaScript: How do you sort an array that includes NaN's

对着背影说爱祢 提交于 2021-02-18 23:36:04

问题


I'm trying to sort an array that sometimes has Infinity or NaN. When I use a standard JavaScript array.sort() it seems to sort until it reaches a NaN and then I get random results after that.

var array =[.02,.2,-.2,Nan,Infinity,20];

Is there a way to still sort this so that the end result is from negative to positive and still have NaN or Infinity at the end.

-.2,.02,.2,20,NaN,Infinity

回答1:


You can catch NaN and Infinity using JavaScript's built-in utility functions for those cases:

let array = [Infinity, -1, 6, 1, 0, NaN, 0, -1, 2, 5, 10, -Infinity, NaN, Infinity, NaN]



//sort -Infinity, NaN, Infinity to the end in random order
array.sort(function(a,b){
  if(isFinite(a-b)) {
    return a-b; 
  } else {
    return isFinite(a) ? -1 : 1;
  }
});

//[-1,-1,0,0,1,2,5,6,10,NaN,Infinity,Infinity,NaN,-Infinity,NaN]
console.log(array);



//sort -Infinity<0<Infinity<NaN
array.sort(function(a,b){
  if(isNaN(a)) { 
    return 1-isNaN(b);
  } else {
    return a-b; 
  }
});

//[-Infinity,-1,-1,0,0,1,2,5,6,10,Infinity,Infinity,NaN,NaN,NaN]
console.log(array);



回答2:


If you just want to bump them to the end in a random order:

var arr = [-1, 0, 1, 10, NaN, 2, NaN, 0, -1, NaN, 5, Infinity, 0, -Infinity];

arr.sort(function(a,b){
    if( !isFinite(a) && !isFinite(b) ) {
        return 0;
    }
    if( !isFinite(a) ) {
        return 1;
    }
    if( !isFinite(b) ) {
        return -1;
    }
    return a-b;
});
//[-1, -1, 0, 0, 0, 1, 2, 5, 10, NaN, NaN, NaN, Infinity, -Infinity]

If you want to also sort the infinities at the end:

var arr = [-1, 0, 1, 10, NaN, 2, NaN, 0, -1, NaN, 5, Infinity, 0, -Infinity];

arr.sort(function(a,b){
    if( !isFinite(a) && !isFinite(b) ) {
        return ( isNaN(a) && isNaN(b) )
            ? 1
            : a < b
                ? -1
                : a === b
                    ? 0
                    : 1;
    }
    if( !isFinite(a) ) {
        return 1;
    }
    if( !isFinite(b) ) {
        return -1;
    }
    return a-b;
});

//[-1, -1, 0, 0, 0, 1, 2, 5, 10, -Infinity, Infinity, NaN, NaN, NaN]

Here the order is -Infinity < Infinity < NaN




回答3:


Negative infinity should logically be ordered first, as it is effectively smaller than all other numbers.

I would thus do it like this:

const cmp = (a,b) => a-b || isNaN(a)-isNaN(b);

// Example
const arr = [Infinity, NaN, Infinity, -Infinity, NaN, 1, 0, NaN, -1, -0];
console.log(arr.sort(cmp));



回答4:


Something like this?

var arr = [-1, 0, 1, 10, NaN, 2, NaN, 0, -1, NaN, 5, Infinity, 0];
function sortInf (a, b) {
    a = parseFloat(a);
    b = parseFloat(b);
    if ((!a || a === -Infinity) && a !== 0) {
        return 1;
    } else if ((!b || b === -Infinity) && b !== 0) {
        return -1;
    } else return a - b;
}
alert(arr.sort(sortInf));



回答5:


a simple and fast way without conditional or function overhead:

var r=[1,9,NaN,3,4,5,0,-4, NaN , 4, Infinity, 7, 2];
 r.sort(function(a,b,c){return  a-b || (a||Infinity)-(b||Infinity) || 0 });
 alert(r) // == -4,0,1,2,3,4,4,5,7,9,NaN,NaN,Infinity

EDIT: updated based on feedback to avoid a NaN return.

this executes about 20X faster than the other answers, so it's ideal if you need perf, and sorting is one area where perf often DOES matter...



来源:https://stackoverflow.com/questions/17557807/javascript-how-do-you-sort-an-array-that-includes-nans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!