C# List Sort()

Description

List Sort() sorts the elements in the entire List using the default comparer.

Syntax


public void Sort()

Example


using System;//w w  w.  ja v a  2 s .c  o  m
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 »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack