Sort list by field (C#)

后端 未结 8 1244
一向
一向 2021-01-01 09:09

I have class like:

class SortNode
{
    public Int32 m_valRating = 0;

    public SortNode(Int32 valRating)
    {
        this.m_valRating = valRating;
    }         


        
8条回答
  •  没有蜡笔的小新
    2021-01-01 09:52

    Use Linq order by.

    var mySortedList = refSortNodeList.OrderBy(x => x.m_valRating);
    

    Here is a real live example where I am pulling a list from a database but it is exactly the same concept.

     vendorProducts = (from vp in db.COMPANIES_VND_PRODUCTS
                        join p in db.CT_CT_INV_CLASSES on vp.CLASS_ID equals p.CLASS_ID
                        join m in db.CT_CT_MODALITY_CODES on vp.MODALITY_ID equals m.MODALITY_ID
                        where vp.COMPANY_ID == companyId
                        select new ProductTypeModality
                        {
                          Active = p.ACTIVE.Equals("Y") ? true : false,
                          BioMedImaging = p.BIOMED_IMAGING,
                          Code = p.CLASS_CODE,
                          Description = p.DESCRIPTION,
                          Id = p.CLASS_ID,
                          PricingMargin = p.PRICING_MARGIN,
                          ModalityCode = m.MODALITY_CODE,
                          ModalityId = m.MODALITY_ID,
                          VendorId = companyId
                        }).OrderBy(x => x.Code).ToList();
    

提交回复
热议问题