JavaScript: How do you sort an array that includes NaN's
问题 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