sorting arrays..

Seems like a simple enough question but I have an array of strings I want sorted.
The strings are number 1 through 99…
However they come out 1, 10…19, 2, 21…29, 3
Not exactly according to plan :slight_smile:

So how do I get this to sort 1, 2, 3… 10…29…

Create a shadow array of integers and use SortWith.

Am I imagining things… the integer array seems sorted just like the string array.
I have said ikeys.Sort yet the debugger seems to show ikeys as being incorrectly sorted.
What am I missing.

if it is a string

[code] dim StringArr() as String = Array(“2”, “1”, “12”, “42”, “23”, “3”, “7”, “45”)

dim IntegerArr() as Integer
redim IntegerArr( StringArr.Ubound )

for i as Integer = 0 to StringArr.Ubound
IntegerArr( i ) = Val( StringArr( i ))
next i

IntegerArr.SortWith( StringArr )
MsgBox join(StringArr)[/code]

Your code is

ikeys.Sort    // great!  It's sorted numerically.
keys.SortWith(ikeys)     // oops!  You just resorted it by string, no longer numeric.

I suspect what you meant was

ikeys.SortWith(keys)

Thanks all.
Seems counter intuitive to me but… that’s the way it is.
Also I took the keys out of a dictionary and apparently there is no guarantee that they are in order or the order I want.

I hate the way these darn computers do what you tell them to do, not what you want them to do. :wink:

It can be confusing. It helps me to remember that there can be only one array on the left hand side, but many arrays on the right hand side. You can only sort one array, so it has to be the one on the left.

array1.SortWith(array2, array3, array4, etc…)

It sorts array1 and orders array2, 3, 4, etc., in the same order as array1.

Tim:

I am usure that I understand correctly.

Say I want to sort an arry of FolderItems.

if I create another array using the FolderItem.Name (so Strings), I can use:

Array_Names.SortWith(Array_FolderItems)

Array_Names holds FolderItem’s Names (its type is String)
Array_FolderItems holds FolderItems (its type is FolderItem)

and I will get my FolderItem array sorted by Names ?

Am I right ?

Emile, Rick wrote you some code here:

https://forum.xojo.com/23368-os-x-10-10-4cpb-windows-8-1-differences/

Does that not work?

Peter:

Thank you for your answer. I started to work with Rick’s code, then… I was disturbed then I forgot to go back to it.

I will do.

BTW: I do not recall Rick solution follows that way (if my understanding and recall are correct).

[quote=198197:@Emile Schwarz] I will get my FolderItem array sorted by Names ?

Am I right ?[/quote]
Correct.

Thank you.