Search code examples
c#listsortingtlist

Sort or Order TList<myobject>


I have a TList<SKU> that I want to sort by the SKU object id's. Below is how the list is obtained from the database (base code from the third party vendor being used).

TList<SKU> skuList = skuAdmin.GetByProductID(this.itemId); *the itemId is an integer value representing the product id.

I want to sort this list based on the the SKUID attribute that every SKU has. I've been looking at the sorts & orders for List<> but I can't seem to find a solution that I can tailor to my need.

This: Sorting TList<object> (ntiers) with ThenBy was probably the closest I could find to what I want to do.

What I want to do is return the SKU list item with the lowest the SKUID can anyone help me out?


Solution

  • If you want to order the whole list.. you can do:

    list = list.OrderBy(x => x.SKUID).ToList();
    

    If you want the lowest one, you order them, then take the first one:

    var first = list.OrderBy(x => x.SKUID).FirstOrDefault(); 
    // will be null or the SKU with lowest SKUID.