'get last alphabetic value without using lists
I have some strings, and I would like to get the letter that is last in alphabetic order.
I know how to do it on a List<string> (ordering the list), but what I would like is such a function :
private string getLastValue(string a, string b)
{
//????
}
getLastValue("a","b") may return "b"
getLastValue("azerty","qwerty") may return "qwerty"
Solution 1:[1]
You can just compare strings with a help of StringComparer, e.g.
private string getLastValue(string a, string b) =>
StringComparer.Ordinal.Compare(a, b) > 0 ? a : b;
Note, that you can choose the comparer required: Ordinal, OrdinalIgnoreCase etc.
Solution 2:[2]
using System.Linq;
var data = new string[] {"azerty","qwerty"};
Console.WriteLine(data.Max());
or
var a = "azerty";
var b = "qwerty";
var inv = StringComparer.InvariantCulture;
Console.WriteLine((inv.Compare(a, b) < 0 ? b : a));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitry Bychenko |
| Solution 2 |
