[ACCEPTED]-Is there a way of using orderby in a forloop C#?-sql-order-by

Accepted answer
Score: 35

Try this:

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

0

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

0

Score: 1

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

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

// add data to aList

aList.OrderBy(x=>x.MyStringProperty);
Score: 0

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

More Related questions