0

I'm sure this is an FAQ, but the first 12 items Google returned do not have this question in it.

I have a SortedDictionary(of String, String) where the first item is an apartment number and the second a tenant name. I populare, then loop over the results and print, and I get something like...

A-10  Bob Smith
A-12  John Taylor
A-1   Alice Monroe
A-2   Janice O'Dell

I am at a complete loss as to what this sorting is trying to do, but in any event, I need this list to be A-1, A-2, A-10, A-12 (etc.)

We are forbidden from using LINQ (above my pay grade), so is there some easy way to change the sorting to be as expected?

Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
  • 1
    I believe you can construct the SortedDictionary with a custom comparer that does what you want. Writing that is left as an exercise for the reader ;) – 500 - Internal Server Error Jun 24 '21 at 17:13
  • "custom comparer" is what I needed, thanks! – Maury Markowitz Jun 24 '21 at 17:14
  • holy crap that's a lot of crap to do something that seems like the default. Do you know why it sorts this odd way in the first place? Since the 1980s I've been told shorter strings aways to the top. – Maury Markowitz Jun 24 '21 at 17:15
  • 3
    If shorter strings always went to the top, “Bob” would come before “Alice.” – John Wu Jun 24 '21 at 17:17
  • @MauryMarkowitz you should get something like `A-1, A-10, A-12, A-2` not that order you are showing. – Guru Stron Jun 24 '21 at 17:21
  • 2
    And if you’ve been using computers since the 80s, I am absolutely gobsmacked you’ve never encountered this sorting issue before. This is why string representations of numbers are often left padded with zeros. – John Wu Jun 24 '21 at 17:21
  • Tip: this is known as “natural order” and there are several answers here for how to implement it, including calling the same function windows uses https://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp – Anders Forsgren Jun 24 '21 at 17:23
  • @AndersForsgren - indeed, this is a duplicate – Maury Markowitz Jun 24 '21 at 17:25

0 Answers0