9.6.1 Implement IComparable interface
Like all collection classes, the list implements the sort () method, which allows you to sort all objects that implement the IComparable interface. In the next example, you will modify the employee class to implement IComparable:
public class Employee:icomparable<employee>
To implement the Icomparable<employee> interface, the Employee object must provide a CompareTo () method:
public int CompareTo(Employee rhs)
{
return this.empID.CompareTo(rhs.empID);
}
The CompareTo () method makes the employee a parameter. We know that using employee is because this is a collection of type safety. The current employee object must compare itself to the employee passed in as the parameter, if it returns-1, indicating that it is less than the argument, or 1 if it is greater than the argument, and returns 0 if the two are equal. This means that the decision is greater than, less than, and equal to employee. In this case, you delegate member Empid for comparison. The Empid member is an int type and uses the default CompareTo () method of the integer type to compare the size between two values.
The System.Int32 class implements the Icomparable<int32> interface, so you can delegate the comparison responsibility to the integer type.
You are now ready to sort the list of employee arrays (emplist), and you need to randomly add integers and employee instances to their respective arrays in order to see if the sorting is working correctly. Create a random number, you need to instantiate the random class, and the next () method that calls the random object produces random numbers. The Next () method is an overloaded method; A version allows you to pass an integer value representing the maximum number of random numbers you want. In this case, you will pass 10来 to produce a random number between 0 and 10: The parameter is 10 and the maximum random number can only be 9.
Random r = new Random ();
R.next (10);
Example 9-14 creates an integer array and an employee array, fills them with random numbers, and prints their values. Then sort the array and print the new value.
Example 9-14 sort integers and employee arrays
using System;
using System.Collections.Generic;
using System.Text;
namespace IComparable
{
//A simple class for storing in an array
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;
}
}
//Employee uses the integer default CompareTo method
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> ();
//The random number of IDs that generate integers and employee
Random r = new Random ();
//Fill array
for (int i = 0; i < 5; i++)
{
//Add the ID of the random employee
Emparray.add (New Employee (R.next (10) + 100));
//Add a random integer
Intarray.add (R.next (10));
}
Displays all content in an integral array
for (int i = 0; i < Intarray.count; i++)
{
Console.Write ("{0}", Intarray[i]. ToString ());
}
Console.WriteLine ("\ n");
//Displays all content in the employee array
for (int i = 0; i < Emparray.count; i++)
{
Console.Write ("{0}", Emparray[i]. ToString ());
}
Console.WriteLine ("\ n");
//integer array sort
Intarray.sort ();
for (int i = 0; i < Intarray.count; i++)
{
Console.Write ("{0}", Intarray[i]. ToString ());
}
Console.WriteLine ("\ n");
//Employee array sort and display
//The following two sentences of the original text should be commented out, it is still useless to
//employee.employeecomparer C = employee.getcomparer ();
//emparray.sort (c);
Emparray.sort ();
//Displays all content in the employee array
for (int i = 0; i < Emparray.count; i++)
{
Console.Write ("{0}", Emparray[i]. ToString ());
}
Console.WriteLine ("\ n");
}
}
}