Supose you have an int Key, that when shown and sorted together with other keys will be, of course, sorted following the natural numbers order.
So, to obtain the “Inverse” you can program…
int InverseSortKey = Int32.MaxValue - ArbitraryIntKey;
Question is: How to get a similar “inverse” value, but for strings?
Example:
string InverseSortKey = "ZZZZZZZZZ" - ArbirtraryStringKey; // Of course this don't compile.
I hope you get the idea.
Naive solution (updated – works if you know the maximum length of all the strings)
Does not take into account the fact that string sorting may vary by culture and does not necessarily match the numerical values of Chars. A full solution would take an ordered list of all the
Charsvalid character sequences (in the particular expected encoding of these strings) by sort order, and then for each char pick the one that’s the reverse.Further disclaimer: Don’t use this solution unless you can assume that the strings are single-byte encoded and sorted exactly by numerical order.