<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FCollections_Data_Structure%2FCompare</id>
		<title>Csharp/C Sharp/Collections Data Structure/Compare - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FCollections_Data_Structure%2FCompare"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Collections_Data_Structure/Compare&amp;action=history"/>
		<updated>2026-04-29T23:47:34Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Collections_Data_Structure/Compare&amp;diff=650&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Collections_Data_Structure/Compare&amp;diff=650&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:18Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Collections_Data_Structure/Compare&amp;diff=651&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Collections_Data_Structure/Compare&amp;diff=651&amp;oldid=prev"/>
				<updated>2010-05-26T11:39:20Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Implement IComparable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
// Implement IComparable. &lt;br /&gt;
 &lt;br /&gt;
using System; &lt;br /&gt;
using System.Collections; &lt;br /&gt;
 &lt;br /&gt;
class Inventory : IComparable { &lt;br /&gt;
  string name; &lt;br /&gt;
  double cost; &lt;br /&gt;
  int onhand; &lt;br /&gt;
 &lt;br /&gt;
  public Inventory(string n, double c, int h) { &lt;br /&gt;
    name = n; &lt;br /&gt;
    cost = c; &lt;br /&gt;
    onhand = h; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  public override string ToString() { &lt;br /&gt;
    return &lt;br /&gt;
      String.Format(&amp;quot;{0,-10}Cost: {1,6:C}  On hand: {2}&amp;quot;, &lt;br /&gt;
                    name, cost, onhand); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  // Implement the IComparable interface. &lt;br /&gt;
  public int CompareTo(object obj) { &lt;br /&gt;
    Inventory b; &lt;br /&gt;
    b = (Inventory) obj; &lt;br /&gt;
    return name.rupareTo(b.name); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
public class IComparableDemo { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    ArrayList inv = new ArrayList(); &lt;br /&gt;
     &lt;br /&gt;
    // Add elements to the list &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Pliers&amp;quot;, 5.95, 3)); &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Wrenches&amp;quot;, 8.29, 2));    &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Hammers&amp;quot;, 3.50, 4)); &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Drills&amp;quot;, 19.88, 8)); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inventory list before sorting:&amp;quot;); &lt;br /&gt;
    foreach(Inventory i in inv) { &lt;br /&gt;
      Console.WriteLine(&amp;quot;   &amp;quot; + i); &lt;br /&gt;
    } &lt;br /&gt;
    Console.WriteLine(); &lt;br /&gt;
 &lt;br /&gt;
    // Sort the list. &lt;br /&gt;
    inv.Sort(); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inventory list after sorting:&amp;quot;); &lt;br /&gt;
    foreach(Inventory i in inv) { &lt;br /&gt;
      Console.WriteLine(&amp;quot;   &amp;quot; + i); &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==implements the IComparable interface==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
public class NewOrderedName : IComparable {&lt;br /&gt;
  private String firstName;&lt;br /&gt;
  private String lastName;&lt;br /&gt;
  public NewOrderedName(String f, String l) {&lt;br /&gt;
     firstName = f;&lt;br /&gt;
     lastName = l;&lt;br /&gt;
  }&lt;br /&gt;
  public int CompareTo(Object o) {&lt;br /&gt;
    NewOrderedName name = (NewOrderedName)o;&lt;br /&gt;
    int lastResult = lastName.rupareTo(name.lastName);&lt;br /&gt;
    if (lastResult != 0)&lt;br /&gt;
      return lastResult;&lt;br /&gt;
    else {&lt;br /&gt;
      int firstResult = firstName.rupareTo(name.firstName);&lt;br /&gt;
      if (firstResult != 0)&lt;br /&gt;
         return firstResult;&lt;br /&gt;
      else&lt;br /&gt;
         return 1;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    NewOrderedName jAdams = new NewOrderedName(&amp;quot;J&amp;quot;, &amp;quot;A&amp;quot;);&lt;br /&gt;
    NewOrderedName jqAdams = new NewOrderedName(&amp;quot;A&amp;quot;, &amp;quot;Q&amp;quot;);&lt;br /&gt;
    NewOrderedName hAdams = new NewOrderedName(&amp;quot;H&amp;quot;, &amp;quot;S&amp;quot;);&lt;br /&gt;
    Console.WriteLine (&amp;quot;jAdams vs. jqAdams {0}&amp;quot;, jAdams.rupareTo(jqAdams)); &lt;br /&gt;
    Console.WriteLine (&amp;quot;jAdams vs. hAdams {0}&amp;quot;, jAdams.rupareTo(hAdams)); &lt;br /&gt;
    Console.WriteLine (&amp;quot;hAdams vs. hAdams {0}&amp;quot;, hAdams.rupareTo(hAdams)); &lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sorting and Searching:Advanced Use of Hashes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
public class AdvancedUseofHashes&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Employee herb = new Employee(&amp;quot;H&amp;quot;, 5);&lt;br /&gt;
        Employee george = new Employee(&amp;quot;G&amp;quot;, 1);&lt;br /&gt;
        Employee frank = new Employee(&amp;quot;F&amp;quot;, 2);&lt;br /&gt;
        Hashtable employees = &lt;br /&gt;
        new Hashtable(Employee.HashByName, Employee.SortByName);&lt;br /&gt;
        employees.Add(herb, &amp;quot;AA&amp;quot;);&lt;br /&gt;
        employees.Add(george, &amp;quot;BB&amp;quot;);&lt;br /&gt;
        employees.Add(frank, &amp;quot;CC&amp;quot;);&lt;br /&gt;
        Employee herbClone = new Employee(&amp;quot;H&amp;quot;, 000);&lt;br /&gt;
        string address =(string) employees[herbClone];&lt;br /&gt;
        Console.WriteLine(&amp;quot;{0} lives at {1}&amp;quot;, herbClone, address);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Employee: IComparable&lt;br /&gt;
{&lt;br /&gt;
    public Employee(string name, int id)&lt;br /&gt;
    {&lt;br /&gt;
        this.name = name;&lt;br /&gt;
        this.id = id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int IComparable.rupareTo(object obj)&lt;br /&gt;
    {&lt;br /&gt;
        Employee emp2 = (Employee) obj;&lt;br /&gt;
        if (this.id &amp;gt; emp2.id)&lt;br /&gt;
        return(1);&lt;br /&gt;
        if (this.id &amp;lt; emp2.id)&lt;br /&gt;
        return(-1);&lt;br /&gt;
        else&lt;br /&gt;
        return(0);&lt;br /&gt;
    }&lt;br /&gt;
    public override int GetHashCode()&lt;br /&gt;
    {&lt;br /&gt;
        return(id);&lt;br /&gt;
    }&lt;br /&gt;
    public static IComparer SortByName&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return((IComparer) new SortByNameClass());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static IComparer SortById&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return((IComparer) new SortByIdClass());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public static IHashCodeProvider HashByName&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return((IHashCodeProvider) new HashByNameClass());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(name + &amp;quot;:&amp;quot; + id);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class SortByNameClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(String.rupare(emp1.name, emp2.name));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class SortByIdClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(((IComparable) emp1).rupareTo(obj2));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    class HashByNameClass: IHashCodeProvider&lt;br /&gt;
    {&lt;br /&gt;
        public int GetHashCode(object obj)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp = (Employee) obj;&lt;br /&gt;
            return(emp.name.GetHashCode());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    string    name;&lt;br /&gt;
    int    id;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sorting and Searching:IComparer as a Property==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
public class IComparerasaProperty&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Employee[] arr = new Employee[4];&lt;br /&gt;
        arr[0] = new Employee(&amp;quot;A&amp;quot;, 1);&lt;br /&gt;
        arr[1] = new Employee(&amp;quot;B&amp;quot;, 2);&lt;br /&gt;
        arr[2] = new Employee(&amp;quot;C&amp;quot;, 4);&lt;br /&gt;
        arr[3] = new Employee(&amp;quot;D&amp;quot;, 3);&lt;br /&gt;
        &lt;br /&gt;
        Array.Sort(arr, Employee.SortByName);&lt;br /&gt;
        // employees is now sorted by name&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arr)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        Array.Sort(arr, Employee.SortById);&lt;br /&gt;
        // employees is now sorted by id&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arr)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        ArrayList arrList = new ArrayList();&lt;br /&gt;
        arrList.Add(arr[0]);&lt;br /&gt;
        arrList.Add(arr[1]);&lt;br /&gt;
        arrList.Add(arr[2]);&lt;br /&gt;
        arrList.Add(arr[3]);&lt;br /&gt;
        arrList.Sort(Employee.SortByName);&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arrList)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        arrList.Sort();    // default is by id&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arrList)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Employee: IComparable&lt;br /&gt;
{&lt;br /&gt;
    public Employee(string name, int id)&lt;br /&gt;
    {&lt;br /&gt;
        this.name = name;&lt;br /&gt;
        this.id = id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int IComparable.rupareTo(object obj)&lt;br /&gt;
    {&lt;br /&gt;
        Employee emp2 = (Employee) obj;&lt;br /&gt;
        if (this.id &amp;gt; emp2.id)&lt;br /&gt;
        return(1);&lt;br /&gt;
        if (this.id &amp;lt; emp2.id)&lt;br /&gt;
        return(-1);&lt;br /&gt;
        else&lt;br /&gt;
        return(0);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static IComparer SortByName&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return((IComparer) new SortByNameClass());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static IComparer SortById&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return((IComparer) new SortByIdClass());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(name + &amp;quot;:&amp;quot; + id);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class SortByNameClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(String.rupare(emp1.name, emp2.name));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class SortByIdClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(((IComparable) emp1).rupareTo(obj2));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    string    name;&lt;br /&gt;
    int    id;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sorting and Searching:Implementing IComparable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
public class ImplementingIComparable {&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Employee[] arr = new Employee[4];&lt;br /&gt;
        arr[0] = new Employee(&amp;quot;A&amp;quot;, 1);&lt;br /&gt;
        arr[1] = new Employee(&amp;quot;B&amp;quot;, 2);&lt;br /&gt;
        arr[2] = new Employee(&amp;quot;C&amp;quot;, 4);&lt;br /&gt;
        arr[3] = new Employee(&amp;quot;D&amp;quot;, 3);&lt;br /&gt;
        &lt;br /&gt;
        Array.Sort(arr);&lt;br /&gt;
        foreach (Employee emp in arr)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        // Find employee id 2 in the list;&lt;br /&gt;
        Employee employeeToFind = new Employee(null, 2);&lt;br /&gt;
        int index = Array.BinarySearch(arr, employeeToFind);&lt;br /&gt;
        if (index != -1)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Found: {0}&amp;quot;, arr[index]);    &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Employee: IComparable&lt;br /&gt;
{&lt;br /&gt;
    public Employee(string name, int id)&lt;br /&gt;
    {&lt;br /&gt;
        this.name = name;&lt;br /&gt;
        this.id = id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int IComparable.rupareTo(object obj)&lt;br /&gt;
    {&lt;br /&gt;
        Employee emp2 = (Employee) obj;&lt;br /&gt;
        if (this.id &amp;gt; emp2.id)&lt;br /&gt;
        return(1);&lt;br /&gt;
        if (this.id &amp;lt; emp2.id)&lt;br /&gt;
        return(-1);&lt;br /&gt;
        else&lt;br /&gt;
        return(0);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(String.Format(&amp;quot;{0}:{1}&amp;quot;, name, id));&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    string    name;&lt;br /&gt;
    int    id;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Sorting and Searching:Using IComparer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
public class SortingandSearchingUsingIComparer1&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Employee[] arr = new Employee[4];&lt;br /&gt;
        arr[0] = new Employee(&amp;quot;A&amp;quot;, 1);&lt;br /&gt;
        arr[1] = new Employee(&amp;quot;B&amp;quot;, 2);&lt;br /&gt;
        arr[2] = new Employee(&amp;quot;C&amp;quot;, 4);&lt;br /&gt;
        arr[3] = new Employee(&amp;quot;D&amp;quot;, 3);&lt;br /&gt;
        &lt;br /&gt;
        Array.Sort(arr, (IComparer) new Employee.SortByNameClass());&lt;br /&gt;
        // employees is now sorted by name&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arr)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        Array.Sort(arr, (IComparer) new Employee.SortByIdClass());&lt;br /&gt;
        // employees is now sorted by id&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arr)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        ArrayList arrList = new ArrayList();&lt;br /&gt;
        arrList.Add(arr[0]);&lt;br /&gt;
        arrList.Add(arr[1]);&lt;br /&gt;
        arrList.Add(arr[2]);&lt;br /&gt;
        arrList.Add(arr[3]);&lt;br /&gt;
        arrList.Sort((IComparer) new Employee.SortByNameClass());&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arrList)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
        &lt;br /&gt;
        arrList.Sort();    // default is by id&lt;br /&gt;
        &lt;br /&gt;
        foreach (Employee emp in arrList)&lt;br /&gt;
        Console.WriteLine(&amp;quot;Employee: {0}&amp;quot;, emp);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Employee: IComparable&lt;br /&gt;
{&lt;br /&gt;
    public Employee(string name, int id)&lt;br /&gt;
    {&lt;br /&gt;
        this.name = name;&lt;br /&gt;
        this.id = id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int IComparable.rupareTo(object obj)&lt;br /&gt;
    {&lt;br /&gt;
        Employee emp2 = (Employee) obj;&lt;br /&gt;
        if (this.id &amp;gt; emp2.id)&lt;br /&gt;
        return(1);&lt;br /&gt;
        if (this.id &amp;lt; emp2.id)&lt;br /&gt;
        return(-1);&lt;br /&gt;
        else&lt;br /&gt;
        return(0);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return(name + &amp;quot;:&amp;quot; + id);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public class SortByNameClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(String.rupare(emp1.name, emp2.name));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public class SortByIdClass: IComparer&lt;br /&gt;
    {&lt;br /&gt;
        public int Compare(object obj1, object obj2)&lt;br /&gt;
        {&lt;br /&gt;
            Employee emp1 = (Employee) obj1;&lt;br /&gt;
            Employee emp2 = (Employee) obj2;&lt;br /&gt;
            &lt;br /&gt;
            return(((IComparable) emp1).rupareTo(obj2));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    string    name;&lt;br /&gt;
    int    id;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use IComparer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
C#: The Complete Reference &lt;br /&gt;
by Herbert Schildt &lt;br /&gt;
Publisher: Osborne/McGraw-Hill (March 8, 2002)&lt;br /&gt;
ISBN: 0072134852&lt;br /&gt;
*/&lt;br /&gt;
// Use IComparer. &lt;br /&gt;
 &lt;br /&gt;
using System; &lt;br /&gt;
using System.Collections; &lt;br /&gt;
 &lt;br /&gt;
// Create an IComparer for Inventory objects. &lt;br /&gt;
class CompInv : IComparer { &lt;br /&gt;
  // Implement the IComparable interface. &lt;br /&gt;
  public int Compare(object obj1, object obj2) { &lt;br /&gt;
    Inventory a, b; &lt;br /&gt;
    a = (Inventory) obj1; &lt;br /&gt;
    b = (Inventory) obj2; &lt;br /&gt;
    return a.name.rupareTo(b.name); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class Inventory { &lt;br /&gt;
  public string name; &lt;br /&gt;
  double cost; &lt;br /&gt;
  int onhand; &lt;br /&gt;
 &lt;br /&gt;
  public Inventory(string n, double c, int h) { &lt;br /&gt;
    name = n; &lt;br /&gt;
    cost = c; &lt;br /&gt;
    onhand = h; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  public override string ToString() { &lt;br /&gt;
    return &lt;br /&gt;
      String.Format(&amp;quot;{0,-10}Cost: {1,6:C}  On hand: {2}&amp;quot;, &lt;br /&gt;
                    name, cost, onhand); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
public class MailList { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    CompInv comp = new CompInv(); &lt;br /&gt;
    ArrayList inv = new ArrayList(); &lt;br /&gt;
     &lt;br /&gt;
    // Add elements to the list &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Pliers&amp;quot;, 5.95, 3)); &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Wrenches&amp;quot;, 8.29, 2));    &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Hammers&amp;quot;, 3.50, 4)); &lt;br /&gt;
    inv.Add(new Inventory(&amp;quot;Drills&amp;quot;, 19.88, 8)); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inventory list before sorting:&amp;quot;); &lt;br /&gt;
    foreach(Inventory i in inv) { &lt;br /&gt;
      Console.WriteLine(&amp;quot;   &amp;quot; + i); &lt;br /&gt;
    } &lt;br /&gt;
    Console.WriteLine(); &lt;br /&gt;
 &lt;br /&gt;
    // Sort the list using an IComparer. &lt;br /&gt;
    inv.Sort(comp); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Inventory list after sorting:&amp;quot;); &lt;br /&gt;
    foreach(Inventory i in inv) { &lt;br /&gt;
      Console.WriteLine(&amp;quot;   &amp;quot; + i); &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>