Generics--Implement IComparable interface

Source: Internet
Author: User
Tags array sort arrays comparison sort tostring

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&lt;employee&gt;


  {


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&lt;employee&gt; Emparray = new list&lt;employee&gt; ();


list&lt;int32&gt; intarray = new list&lt;int32&gt; ();


//The random number of IDs that generate integers and employee


Random r = new Random ();


//Fill array


for (int i = 0; i &lt; 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 &lt; Intarray.count; i++)


     {


Console.Write ("{0}", Intarray[i]. ToString ());


     }


Console.WriteLine ("\ n");


//Displays all content in the employee array


for (int i = 0; i &lt; Emparray.count; i++)


     {


Console.Write ("{0}", Emparray[i]. ToString ());


     }


Console.WriteLine ("\ n");


//integer array sort


Intarray.sort ();


for (int i = 0; i &lt; 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 &lt; Emparray.count; i++)


     {


Console.Write ("{0}", Emparray[i]. ToString ());


     }


Console.WriteLine ("\ n");


   }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.