Sort the elements in List using the default comparer in CSharp

Description

The following code shows how to sort the elements in List using the default comparer.

Example


using System;/*from  ww  w . j  a v  a  2 s.com*/
using System.Collections.Generic;

public class Employee : IComparable<Employee>
{
    private int empID;

    public Employee(int empID)
    {
        this.empID = empID;
    }

    public override string ToString()
    {
        return empID.ToString();
    }

    public bool Equals(Employee other) 
    {
        if (this.empID == other.empID)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public int CompareTo(Employee rhs)
    {
        return this.empID.CompareTo(rhs.empID);
    }
}
public class Tester
{
    static void Main()
    {
        List<Employee> empArray = new List<Employee>();
        List<Int32> intArray = new List<Int32>();

        for (int i = 0; i < 5; i++)
        {
            empArray.Add(new Employee(i));
            intArray.Add(i);
        }
        intArray.Sort();
        for (int i = 0; i < intArray.Count; i++)
        {
            Console.Write("{0} ", intArray[i].ToString());
        }
        empArray.Sort();
        for (int i = 0; i < empArray.Count; i++)
        {
            Console.Write("{0} ", empArray[i].ToString());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary