I have a dictionary containg ID which are alphanumeric (e.g. a10a10 & d10a9) from which I want the biggest ID, meaning 9 < 10 < a …
When I use the following code, d10a9 is MAX since 9 is sorted before 10
var lsd = new Dictionary<string, string>();
lsd.Add("a", "d10a10");
lsd.Add("b", "d10a9");
string max = lsd.Max(kvp => kvp.Value);
How can I get the Max value of the IDs with the Longest string combined?
One way would be to do this
however I think that this method would evalute the max length for each item so you may be better to extract it to a variable first
If you are going to have null strings in there you may need to perform null checks too
Alternatively treat your string as Base36 number and convert to long for the max function and then convert back again to get the max string.
Finally just just the Agregate extension method instead of Max as this lets you do all the comparison logic….
This could doesn’t have null checks but you easily add them in.