So I'm trying to build a function that receives as a parameter a List of 5 elements, this list is of the string type, and the result that I'm after follows this logic:
List:
List<string> lista= new List<string>();
lista.Add("Movel");
lista.Add("ONU");
lista.Add("Pata");
lista.Add("Tela");
lista.Add("Plasticos");
I need to return only the elements where the last letter is superior to the first following alphabetical order, in this case, only "ONU" and "Plasticos" would be ret开发者_StackOverflow中文版urned.
I'm rather new to C#, in fact, I'm just new to programming overall, but in the small knowledge I've accumulated, I honestly don't know where to go with this, any help would be grand.
Theres probably regex ways you can do this or something, but you can probably do this with a Linq expression of some sort e.g.
IEnumerable<string> temp = from str in lista
let last = str.ToLower().Last()
let first = str.ToLower().First()
where last > first
select str;
Would give you the answers you seek I think, but that's probably a pretty inefficient way to do it if you need a quick algorithm.
return lista.Where(s => { s = s.ToLower(); return s[0] < s[s.Length-1] });
Try:
List<string> listb = lista.FindAll(s => String.Compare(s.Substring(s.Length - 1), s.Substring(0, 1)) == 1).ToList();
I could come up with this
List<string> filtered = lista.FindAll(s => char.ToLowerInvariant(s[s.Length - 1]) > char.ToLowerInvariant(s[0])).ToList();
My result:
var result = lista.Where(x => x.ToLower()[0] < x.ToLower()[x.Length - 1]).ToList();
精彩评论