SortWith Misunderstanding

Clearly my understanding of how SortWith works is flawed.

I have two arrays:

 fileList() as String
 fileSize() as Integer

I want to sort the fileList array alphabetically, so I use:

fileList.Sort

Now I would like to sort the fileSize array in the same order the sorted fileList array is in, so I try:

fileList.SortWith(fileSize)

This does’t work, however.
Can someone help clear up my understanding of how SortWith is supposed to work?

Possibly:

fileSize.SortWith (fileList)

?

Here’s the documentation for SortWith so you don’t have to guess how things work:

If the two arrays are related to each other, you need to SortWith every time.

@Tim_Parnell,
Yes, that’s where I started (naturally).
Unfortunately I came away with the understanding that what I have shown (above) would do what I’m wanting… and it does not. I read the docs and evidently do not understand them correctly.

@TimStreater,
I tried that as well. Also doesn’t sort the fileSize array in the same order as the fileList array.

So a key takeaway is that the base array is the one doing the sort. Both arrays are being sorted every time.

arsNames.SortWith(ariSizes) will sort the sizes array the same way the names array gets sorted.

You must always keep these in sync. The first thing your original post does is separate the association between the names and the sizes by sorting the names array by itself.

1 Like

@Tim_Parnell,

Ah… I get it now.
Once the association is made the associated arrays will then be sorted in the order of the base array. So I make the associations between whatever arrays need to be connected and then sort the original array, and the rest will follow.

Thank you Tim

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.