Sort a C # list based on its element

I have a C # class as follows:

public class ClassInfo {
    public string ClassName;
    public int BlocksCovered;
    public int BlocksNotCovered;


    public ClassInfo() {}

    public ClassInfo(string ClassName, int BlocksCovered, int BlocksNotCovered) 
    {
        this.ClassName = ClassName;
        this.BlocksCovered = BlocksCovered;
        this.BlocksNotCovered = BlocksNotCovered;
    }
}

And I have a list of CIX Class () classes as shown below.

List<ClassInfo> ClassInfoList;

How can I sort a ClassInfoList based on BlocksCovered?

+3
source share
4 answers

This returns a List<ClassInfo>ordered by BlocksCovered:

var results = ClassInfoList.OrderBy( x=>x.BlocksCovered).ToList();

Note that you really have to make property BlocksCovereda, now you have public fields.

+6
source
myList.Sort((x,y) => x.BlocksCovered.CompareTo(y.BlocksCovered)
+6
source

List<T>, Sort(), List<T> .

ClassInfoList.Sort((x, y) => x.BlocksCovered.CompareTo(y.BlocksCovered));

OrderBy() Linq, , List<T>, , List<T> .

+1

Linq, :

ClassInfoList.OrderBy(c => c.ClassName);
0

All Articles