Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

a
b
c
d

looking how to do this, wondered even if i could use linq orderby inside the forloop?

like image 839
Calibre2010 Avatar asked Oct 26 '10 15:10

Calibre2010


2 Answers

Try this:

List<Item> myItems = new List<Item>();
//load myitems
foreach(Item i in myItems.OrderBy(t=>t.name))
{
 //Whatever
}
like image 145
Abe Miessler Avatar answered Nov 10 '22 01:11

Abe Miessler


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);
like image 43
awrigley Avatar answered Nov 10 '22 00:11

awrigley