I understand this is how Javascript works but I still have two
questions that I can't find answers to out on the web:
1) is there something in XUL that can be done to sort with a more
expected result?
2) If not, can anyone point me to a Javascript method that will do
what I want?
Thanks in advance...
This is not "as JavaScript is" but is the difference between String sort
oder and Number sort order. If you sort strings, you get, what you got
but don't like, if you sort numbers, you get, what you like. So you must
very the data used for sorting is of type 'number' not 'string'!
>Currently when sorted, the numbers don't sort like a human would expect. IE - 10 comes before 1 instead of the reverse.
>
>
How are you sorting the numbers? For a bare array of numbers, you would
write something like arr.sort(function(a, b) { return a - b; });
--
Warning: May contain traces of nuts.
George - Prior to sorting, I am doing a parseInt on the number to ensure
it is not a string. Still, result is the same. My current work-around
is to add a zero to the beginning of all single digit numbers (ie - 1
becomes 01), sort, then remove the zero. This gets me what I want.
However, something a bit more elegant is what I am looking for.
Thanks for your help so far...
> Neil - I am doing the arr.sort... call. But that give me the same
> result. 10 comes before 1, 20 before 2, etc.
In the Error Console:
[20, 2, 10, 1].sort(function(a, b) { return a - b; })
evaluates to
[1, 2, 10, 20]
(In fact this works even if you use "20", "2", "10", "1"!)