Edward83
Edward83

Reputation: 6686

Sort list by field (C#)

I have class like:

class SortNode
{
    public Int32 m_valRating = 0;

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

and some list refSortNodeList:

List<SortNode> refSortNodeList = new List<SortNode>();

Random refRandom = new Random();

for (int i = 0; i < 100; ++i)
{
    refSortNodeList.Add(new SortNode(refRandom.Next(-10, 30)));
}

foreach (var varSortNode in refSortNodeList)
{
    Console.WriteLine("SortNode rating is {0}", varSortNode.m_valRating);
}

How to sort easily my refSortNodeList by m_valRating field? Or maybe I need to use some another List class?

Upvotes: 44

Views: 66404

Answers (8)

Kenn
Kenn

Reputation: 2769

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<ProductTypeModality>();

Upvotes: 15

Khairuddin Ni&#39;am
Khairuddin Ni&#39;am

Reputation: 833

List<SortNode> refSortNodeList = new List<SortNode> ();

Random refRandom = new Random ();

for (int i = 0; i < 100; ++i) {
    refSortNodeList.Add (new SortNode (refRandom.Next (-10, 30)));
}

// Use this (Linq) if you're using .NET 3.5 or above.
var sortedList = refSortNodeList.OrderBy (node => node.m_valRating);
foreach (var varSortNode in sortedList) {
    Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating);
}

// Use this otherwise (e.g. .NET 2.0)
refSortNodeList.Sort (
    delegate (SortNode n1, SortNode n2) {
        return n1.m_valRating.CompareTo (n2.m_valRating);
    }
);

foreach (var varSortNode in refSortNodeList) {
    Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating);
}

Upvotes: 0

Mike Marshall
Mike Marshall

Reputation: 7850

Try this:

refSortNodeList.Sort(new delgate(SortNode x, SortNode y)
   {
       return x.CompareTo(y);
    }
);

Upvotes: 1

Jon Hanna
Jon Hanna

Reputation: 113242

In-place:

refSortNodeList.Sort(
  (x, y) =>
    x == null ? (y == null ? 0 : -1)
      : (y == null ? 1 : x.m_valRating.CompareTo(y.m_valRating))
);

Creating a new enumeration:

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

Creating a new list:

var newList = refSortNodeList.OrderBy(x => x.m_valRating).ToList();

In-place is fastest and most memory efficient, but no good if you want to also retain the old list.

The next is faster than the last and gives results as they go, but you have to re-do the sort to use it again, in which case the third is the one to go for.

Upvotes: 21

Kelsey
Kelsey

Reputation: 47726

You can use Linq for basic sorts:

refSortNodeList.OrderBy(n => n.m_valRating);

If you need more complex sorting your will need to implement IComparable to use the built in sorting.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062770

list.Sort((x,y) =>
    x.m_valRating.CompareTo(y.m_valRating));

Upvotes: 83

dcarneiro
dcarneiro

Reputation: 7150

It's easy using linq:

var newlist = refSortNodeList.sort( n => n.m_valRating );

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190945

Implement IComparable<T>

Upvotes: 2

Related Questions