Trying to get the highest and lowest value from an array that I know will contain only integers seems to be harder than I thought.
var numArray = [140000, 104, 99];
numArray = numArray.sort();
alert(numArray)
I'd expect this to show 99, 104, 140000
. Instead it shows 104, 140000, 99
. So it seems the sort is handling the values as strings.
Is there a way to get the sort function to actually sort on integer value?
The question has already been answered, the shortest way is to use
sort()
method. But if you're searching for more ways to sort your array of numbers, and you also love cycles, check the followingInsertion sort
Ascending:
Descending:
Selection sort:
Ascending:
Descending:
Have fun
In the new ES6 world its much easier to do a sort
Thats all you need :)
The reason why the sort function behaves so weird
From the documentation:
If you print the unicode point values of the array then it will get clear.
This returns: "49, 49, 57".
Now, because 140000 and 104 returned the same values (49) it cuts the first index and checks again:
If we sort this, then we will get:
so 104 comes before 140000.
So the final result will be:
104, 140000, 99
Conclusion:
sort()
does sorting by only looking at the first index of the numbers.sort()
does not care if a whole number is bigger than another, it compares the value of the unicode of the digits, and if there are two equal unicode values, then it checks if there is a next digit and compares it as well.To sort correctly, you have to pass a compare function to
sort()
like explained here.Array.prototype.sort() is the go to method for sorting arrays, but there are a couple of issues we need to be aware of.
The sorting order is by default lexicographic and not numeric regardless of the types of values in the array. Even if the array is all numbers, all values will be converted to string and sorted lexicographically.
So should we need to customize the sort() and reverse() method like below.
Referred URL
For sorting numbers inside the array
For reversing numbers inside the array
Referred URL
By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -
EDIT: using ES6 arrow functions:
My personal favorite form of this function allows for a param for Ascending, or Descending:
Usage as simple as:
jsFiddle
Or Code Snippet Example Here!
.smartSort('asc' | 'desc')
Now have even more fun with a sorting method that sorts an array full of multiple items! Doesn't currently cover "associative" (aka, string keys), but it does cover about every type of value! Not only will it sort the multiple values
asc
ordesc
accordingly, but it will also maintain constant "position" of "groups" of values. In other words; ints are always first, then come strings, then arrays (yes, i'm making this multidimensional!), then Objects (unfiltered, element, date), & finally undefineds and nulls!"Why?" you ask. Why not!
Now comes in 2 flavors! The first of which requires newer browsers as it uses
Object.defineProperty
to add the method to theArray.protoype
Object. This allows for ease of natural use, such as:myArray.smartSort('a')
. If you need to implement for older browsers, or you simply don't like modifying native Objects, scroll down to Method Only version.jsFiddle Array.prototype.smartSort('asc|desc')
Use is simple! First make some crazy array like:
Then simply sort it!
Method Only
Same as the preceding, except as just a simple method!
Use:
jsFiddle Method smartSort(Array, "asc|desc")