Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

sortOn() and numeric sorting

Guest
Jun 12, 2008 Jun 12, 2008
Hi there:

I'm using sortOn() to sort me some array items. The first item to sort on is a number (4 digit year), and the second item can be an alphanumeric mix. Example of a (correctly sorted) list (the year is not shown, and sorts correctly anyway):

quote:


myArray({"year","title"});
//e.g.: myArray({"1975","My Secret Freedom No. 1"})

/* a correctly sorted list of the "title":
My Secret Freedom No. 1
My Secret Freedom No. 2
My Secret Freedom No. 3
My Secret Freedom No. 4
My Secret Freedom No. 5
My Secret Freedom No. 6
My Secret Freedom No. 7
My Secret Freedom No. 8
My Secret Freedom No. 9
My Secret Freedom No. 10
My Secret Freedom No. 11
My Secret Freedom No. 12
My Secret Freedom No. 13
My Secret Freedom No. 14
My Secret Freedom No. 15
My Secret Freedom No. 16
My Secret Freedom No. 17
My Secret Freedom No. 18
My Secret Freedom No. 19
My Secret Freedom No. 20
My Secret Freedom No. 21
My Secret Freedom No. 22
*/


Using things like the below give me incorrect sorts, with 10 before 1 and so on.

quote:

myArray.sortOn(["year","title"],[Array.NUMERIC|Array.DESCENDING,Array.NUMERIC]);
//or
myArray.sortOn(["year","title"],[Array.NUMERIC|Array.DESCENDING,Array.CASEINSENSITIVE]);
//or
myArray.sortOn(["year","title"],[Array.NUMERIC|Array.DESCENDING,Array.NUMERIC|Array.CASEINSENSITIVE]);

/* returns something like:
My Secret Freedom No. 12
My Secret Freedom No. 2
My Secret Freedom No. 3
My Secret Freedom No. 4
My Secret Freedom No. 5
My Secret Freedom No. 6
My Secret Freedom No. 7
My Secret Freedom No. 8
My Secret Freedom No. 9
My Secret Freedom No. 10
My Secret Freedom No. 11
My Secret Freedom No. 1
My Secret Freedom No. 13
My Secret Freedom No. 14
My Secret Freedom No. 15
My Secret Freedom No. 16
My Secret Freedom No. 17
My Secret Freedom No. 18
My Secret Freedom No. 19
My Secret Freedom No. 20
My Secret Freedom No. 21
My Secret Freedom No. 22
*/



What obvious thing have I missed?

Thanks in advance!


TOPICS
ActionScript
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 13, 2008 Jun 13, 2008
those years should be numbers, not strings (unless you want string comparison which would probably be ok because all the years probably have the same number of digits):

Translate
Community Expert ,
Jun 12, 2008 Jun 12, 2008
it's sorted by year first, then title. it's not clear there's problem unless you show the years, too.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 12, 2008 Jun 12, 2008
Hi kglad:

Thanks for the response...

It is sorting by "year" correctly. I didn't show "year" in my post, because they are all "1975".

Technically, I'm sure it's sorting the "title" correctly, too -- just not intuitively (ie, 1,2,3,4,5...).

Ideas?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2008 Jun 12, 2008
it should sort them like strings. but if you want numeric sorting on the string after No., use a custom sort function.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 12, 2008 Jun 12, 2008
quote:

Originally posted by: kglad
it should sort them like strings. but if you want numeric sorting on the string after No., use a custom sort function.


Isn't this a custom sort function I'm creating? (will I need to actually sort twice, and compare?)

I want the year sorted numerically. I would like (and assume this is how it would work) the title to be sorted alphabetically. If you have insight as to how to get the titles (which include numbers, yes) to sort in an intuitive ("correct") order, I am most appreciative. Surely someone has run into this issue before?

AS code attached.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2008 Jun 12, 2008
no, that's not a custom sort function. and it makes no sense to sort a string using Array.NUMERIC.

you can sort those strings various ways, but they're still strings and 1 will be followed by 11, not 2, for example.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 12, 2008 Jun 12, 2008
quote:

Originally posted by: kglad
no, that's not a custom sort function. and it makes no sense to sort a string using Array.NUMERIC.

you can sort those strings various ways, but they're still strings and 1 will be followed by 11, not 2, for example.


Hi again kglad:

Well, OK -- this, I think is getting to the actual issue I'm having. So, as to how what you just said applies to my original question: What, then do you suggest I be doing? If simply using sortOn() won't do it, what should I look to do?





Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2008 Jun 13, 2008
use the sort() method with a custom function.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 13, 2008 Jun 13, 2008
Hi again kglad:

I appreciate you responding to me -- but if you could maybe, just a wee bit, expand on your last post -- I would be most appreciative.

quote:

use the sort() method with a custom function.


If using a 'simple' sortOn() isn't going to do the trick for strings which are alphanumeric, but a sort()-plus-custom function will do the trick... What sort of comparison should I be doing in this custom function? This, I think, is the heart of my question which we have been dancing around.

I have looked at sort() from the start, and documentation is woefully thin; The Flash docs don't shed too much light on my particular case, and the "Essential Actionscript 3.0" book doesn't even mention it at all.

If you aren't able or willing to nudge me with a short code example, I understand; and if anyone else has any insight to share, they are welcome to post as well.

Cheers,

Patrick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2008 Jun 13, 2008
those years should be numbers, not strings (unless you want string comparison which would probably be ok because all the years probably have the same number of digits):

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 13, 2008 Jun 13, 2008
Hi kglad:

OK, on the years: Yah, I'll move them to being actual dates (a la Date()), but for now & testing, they're all 4 digits.

Thanks! for the example code on the sort+compare. That was what I was afraid of doing. 😉

These "title" strings are going to be complete unknowns. Some can include Roman numerals, some numbers like the previous examples, and they can be anywhere in the string. (sigh). Regrettably, I won't be able to get client to standardize their input.

For now, I think easiest, I'll add a "sortOrder" attribute to the XML element -- and forcibly put them in order. Unless you see an easy way with what you've posted to account for unknowns?

Cheers,

Patrick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2008 Jun 13, 2008
with the sound titles, you can order them as strings first, and then if the first part of the string (before the numerals) is the same, order them numerically by converting the numeral substring to numbers.

so, you can still order them as you want, no matter the format of the titles.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 14, 2008 Jun 14, 2008
Hi kglad:

OK!

Using your example as a start, I have successfully sorted -- making the split into a regular expression, a la:

-----
var numberSplit:RegExp = /(\D*)/i;
trace(a.title.split(numberSplit))
var aN = (a.title.split(numberSplit)[1]);
-----

I'm starting to see a noticeable performance (speed) hit -- especially when scripting the sort(s) to take names into account such as "My No. 1 Secret Freedom" in addition to names such as "My Secret Freedom No. 1".

Even with currently only 300 records to sort, I'm thinking this will be much more efficient if sorted on the server side before being handed to Flash.

Many thanks for your assistance & pointers.

[edit -- ps: not sound, but artwork: http://www.hungliu.com/test.html ...of note is use of Zoomify component.]
Cheers,


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2008 Jun 14, 2008
LATEST
you're welcome.

and yes, the flash sort method is not very efficient so if you can use server-side coding to sort, that would probably be a better way to proceed.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines