开发者

Is there a way of using orderby in a forloop C#?

开发者 https://www.devze.com 2023-01-21 20:34 出处:网络
I have a for loop where i want to orderby the name alphabetically a b c d looking how to do this, wondered e开发者_如何学Goven if i could use linq orderby inside the forloop?Try this:

I have a for loop where i want to orderby the name alphabetically

a
b
c
d

looking how to do this, wondered e开发者_如何学Goven if i could use linq orderby inside the forloop?


Try this:

List<Item> myItems = new List<Item>();
//load myitems
foreach(Item i in myItems.OrderBy(t=>t.name))
{
 //Whatever
}


new string[] { "d", "c", "b", "a" }
    .OrderBy(s => s)
    .ToList()
    .ForEach(s => MessageBox.Show(s));


You don't need a Loop at all. Just use LINQ:

List<MyClass> aList = new List<MyClass>();

// add data to aList

aList.OrderBy(x=>x.MyStringProperty);


foreach needs an IEnumerable<T> LINQ order-by takes in one IEnumerable<T> and gives you a sorted IEnumerable<T>. So yes, it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消