44

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Edward83
  • 6,664
  • 14
  • 74
  • 102

8 Answers8

82
list.Sort((x,y) =>
    x.m_valRating.CompareTo(y.m_valRating));
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
20

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.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
14

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>();
bluish
  • 26,356
  • 27
  • 122
  • 180
Kenn
  • 2,709
  • 2
  • 29
  • 60
2

Implement IComparable<T>

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

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.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
1

Try this:

refSortNodeList.Sort(new delgate(SortNode x, SortNode y)
   {
       return x.CompareTo(y);
    }
);
Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
0

It's easy using linq:

var newlist = refSortNodeList.sort( n => n.m_valRating );
dcarneiro
  • 7,060
  • 11
  • 51
  • 74
0
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);
}
Khairuddin Ni'am
  • 823
  • 1
  • 8
  • 13