SortWith when base array not ALL unique

If you are going to use the SortWith command, it makes sense to me that when there are some elements of the base array that are not unique that exactly what order those rows will be in the sorted version is “undefined”. But I would hope that the command would nonetheless work within this limitation. But if you insist that the base array only contain only unique values, it seems that the usefulness of this command is greatly reduced. The example in the documentation is sorting some arrays using a Name column. It would seem in many circumstances that the Name array would, for the most part, have unique elements but occasionally there would be duplicates. Does this fact make the command useless?

I am asking to find out if people have experience ignoring the strict rule that the base array have only unique values and using it successfully. Or is this verboten.

I think they are just saying that the ultimate order of those items is undefined. In other words, the sort is not stable.

By those, I assume you mean the small subset of rows that have the “same” name.

One time they (that small subset) might sort one way and another time another way – that is what you mean by [quote]not stable[/quote]–but basically the overall sort by name will be successful.

Yes, that’s what I meant.

You can write your own sort method by passing the address of a delegate method although you’d probably need to implement your own version of sortwith. See

I tend to use classes to store multiple properties rather than multiple arrays that are linked by index and this allows easy sorting by surname->firstname and even descending sorts. The technique pretty well matches how to sort a listbox using the val of the number so is familiar to most of us.

Wayne, I like the simplicity of having the language take care of this relatively common scenario. Rather than having to “understand” the vaguely threating (to me) documentation of SortWith and the limitations of that command.

It would be nice if that command had an option to be “extended” to resolve ties by looking at the next array in the list and then the third etc. if necessary. Other languages accommodate this.

But I was trying to avoid doing too much coding myself although I can probably accomplish this. I doubt my algorithm would be as fast as a built-in one, and when I write things myself I always introduce the anxiety of possibly having screwed up some edge cases.

it would be kind of nice if sortwith had a way to use a delegate to make it so you could extend it in whatever way you wanted like you can with sort

but then the delegate would need to be able to have a paramarray of arrays of unknown types that you would access to resolve things when preceding comparisons all came out “equal”

when I have needed multikey sorting I have

  • resorted to putting things into an in memory db and doing a select with appropriate order by clauses
  • creating the bae array that gets sorted in a fashion it has multiple items in the array to be sorted (perhaps concatenated string values so they WILL sort properly and compare on the “keys” as expected)