Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Problem 1

Status
Not open for further replies.

coder1964

IS-IT--Management
Aug 28, 2003
29
CA
Hi All

I am trying to sort an array using myarray.sort() and it doesnt really work.
I get a kind of sort but things are not right.
For example
[9,2,22,17,43]
after a sort will become
[17,2,22,43,9]

Is this a bug or am not not applying the sort method correctly.

T.I.A.
 
Hey,

I had this problem as well, and think it is because it is sorting it alphabetically as opposed to numerically. It thinks it is a string. I fixed it by storing all my single digit numbers in the array with a leading 0.

So, you would have [09,02,22,17,43] which should sort fine into [02,09,17,22,43]. From there when I was displaying the number I just put them through a function which stripped off the front 0 if it was there.

Hope this helps!
 
it is doing alphabetically however with a little tweak sort works just fine with numbers

use

arr.sort(function(a,b){return a>b})

gives [2,9,17,22,43]
 
Hey Bill,

Can you do that using sortOn() as well? It's what I was using and I'm having to add 0's and take them off when I put in and pull out of the array. I've got it all sorted now, but was wondering if you could do that with sort on?
 
sorton requires a string and the only time i ever use it is when an array contains objects and i want to sort the array on a field

i dont see how it applies to numbers. to sort numbers just use the code i gave which does not need the addition of zeros to turn the numbers into a string

or am i missing something ?
 
Billwatson.

Many thanks. That works well.

Can you tell me why does the actionscript dictionary not explain that this is how you sort numbers?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top