1

(I've inherited a Flex/AIR project and it's been a while since I worked with Flex so forgive me if this is a basic question)

I have a Flex/AIR project which loads a directory of image file names and presents them in a Tree component. The user can then click on a file name to display the image.

I am stumped on why the sort (order of file names) is different in the Flex Tree component view from the (OSX) directory view. OSX is doing the smart thing and sorting on numeric as well as alpha value. The Flex Tree is not being so smart...as the screen shot below indicates.

I've been searching out the spark Sort and SortField collections but am not clear yet how I can use these. Is there a simple solution for doing the kind of sort I need: a way to tell Flex to consider the numeric portion of the file name?

Update:

Guess I am hunting the Snark.. called "natural sort" (sorting-for-humans-natural-sort-order) ...

enter image description here enter image description here

4
  • is it possible that OS X finder was sorting by date, and that the files were created in 'numerical order', therefore yielding what looks like the sort that you are describing? I find it highly unlikely that OS X finder is sorting on partial name numerically. Oct 4, 2012 at 0:29
  • @GreyBeardedGeek - thanks for the reply. I was pretty sure it was sorting on both numeric and alpha and just checked again (and updated the screen shot to show it). It is sorting on name but handles Doc1 and Doc11 etc. intelligently. Pretty cool - but now the client wants me to do that too :-)
    – spring
    Oct 4, 2012 at 0:38
  • 1
    Just tried it on OS X - yes, pretty cool - I didn't realize that it did that. The only thing that I can think of is to parse each file name into text and numeric fields, and then sort, with primary sort on the first field, secondary sort on the second, etc, with each sort being alpha or numeric according to the type of the 'field', with, I think, alpha sorting before numeric when the types differ. Oct 4, 2012 at 0:56
  • GreyBeardedGeek is right. You would have to build a custom comparison function to pass to the sort function.
    – Eduardo
    Oct 4, 2012 at 6:37

1 Answer 1

0

The function to sort 'naturally' already exists in the as3-commons-lang project. In the StringUtils class, look for the naturalCompare() function.

Now all that is left to do, is to apply it to all the collections in your hierarchical structure. You'll have to recursively loop through all the nodes in the tree and apply a sort function to each node's children.

2
  • Thanks - I found that last night after I had learned what that "sort" of sort is called... also found this simpler implementation: as3natcompare (code.google.com/p/as3natcompare).
    – spring
    Oct 4, 2012 at 11:32
  • @skinnyTOD Yeah, I noticed the as3-commons version is an exact port of the Java one: makes sense there's room for improvement.
    – RIAstar
    Oct 4, 2012 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.