Home > Blockchain >  get last alphabetic value without using lists
get last alphabetic value without using lists

Time:04-13

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"

CodePudding user response:

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.

CodePudding user response:

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));
  • Related