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