Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting

0 views
Skip to first unread message

reverendlinux

unread,
Mar 6, 2010, 11:24:51 AM3/6/10
to
I have a XUL app that uses a tree. The first column in the tree is
for an inventory number. These numbers are auto-generated by
Javascript. Currently when sorted, the numbers don't sort like a
human would expect. IE - 10 comes before 1 instead of the reverse.

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...

Georg Maaß

unread,
Mar 6, 2010, 12:35:43 PM3/6/10
to

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'!

Neil

unread,
Mar 6, 2010, 4:46:00 PM3/6/10
to
reverendlinux wrote:

>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.

J Paul Richardson

unread,
Mar 8, 2010, 11:00:51 PM3/8/10
to Neil, dev-te...@lists.mozilla.org
Neil - I am doing the arr.sort... call. But that give me the same
result. 10 comes before 1, 20 before 2, etc.

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

unread,
Mar 9, 2010, 5:03:54 AM3/9/10
to
J Paul Richardson wrote:

> 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"!)

0 new messages