c sharp

C# IComparable

The IComparable interface in the C sharp programming language will be discussed in this article. Using several methods from this interface, we may compare and even order the objects in a class. Because the compiler does not allow it, the comparison and sorting methods are normally not directly applicable to class objects. Instead, the IComparable interface is used to compare and sort class objects. This interface is mainly used when we have created a generic list of objects that we want to sort and compare them. The comparison between objects is done through a method called the CompareTo() function in the C# programming language.

CompareTo() Function of the IComparable Interface:

The CompareTo() function compares the objects of a class in a C# program. The CompareTo() function is called with a return data type like integer or string. But, in the case of object comparison, we will use the integer data type, which will return the zero and non-zero states when the object is compared. The syntax for utilizing this function in the C# programming language is written below:

# “public int CompareTo(Object)”

We’ll implement this interface and its CompareTo() function in the Ubuntu 20.04 environment to better understand this approach.

Example 01: Comparing Objects of an Array Class and Sorting Them in Ubuntu 20.04

In this example, we will be comparing objects of a class and sorting them in ascending order of the ID. The use of the CompareTo() function will be present along with the Sort() function in this example. We will differentiate the sorted and unsorted list of objects before and after the comparison to overlook the mechanism of the IComparable interface. The following code is implemented in the Ubuntu 20.04 environment and executed on the Command line terminal:

Text Description automatically generated

Text Description automatically generated

In the previous code, we have formed an “Array” class. Then, we created some objects from the class and assigned values to them. After this, we used the IComparable interface to use the CompareTo() function to compare the objects we created earlier and sort them. Afterward, we will be using the Sort() function by calling it with the object’s name and printing both the sorted and unsorted list of the array of objects.

After compiling and executing the previous code, we will get the following output:

Text Description automatically generated

The output reflects the difference between the sorted and unsorted list and how the CompareTo() and the Sort() function have worked simultaneously to provide a simplified answer in the IComparable interface.

Example 02: Comparing Objects of an Employee Class and Sorting Them in Ubuntu 20.04

This example will compare instances from the same class and sort them by EmployeeID ascending order. In this example, the CompareTo() method will be utilized with the Sort() function. To avoid overlooking the IComparable interface’s logic, we shall distinguish between a sorted and unsorted list of Employees before and after comparison by printing them both, respectively. The following code was written on the Ubuntu 20.04 environment and will run on the command line terminal:

Text Description automatically generated

Text Description automatically generated

We built an “Employee” class in the given piece of code. Then, we generated some class objects and assigned them values. After that, we utilized the IComparable interface to compare and sort the objects we built previously by using the CompareTo() function. Next, we used the Sort() function with the name of the object of the Employee class and displayed both the sorted and unsorted lists of the Employee class’s objects. We used a foreach loop to move through the list of objects one by one for printing the following lists:

Text Description automatically generated

This will be the output of the previous piece of code when it is compiled and executed on the command line terminal. As we can see in the output, the unsorted list and sorted list of employees based on their ID are displayed on the output screen as the CompareTo() function successfully assessed and helped the Sort() function to sort this list.

Example 03: Using the IComparable Interface for a Class and Its Objects To Modify Them in Ubuntu 20.04

Using the CompareTo() function in the IComparable interface, we devised methods in a class that would be utilized in making a decision or altering a decision in this example. We will initialize the class in this interface to use the comparison and sorting functions in this example. The different states of the CompareTo() function will be addressed in this example to understand this function:

In this given code, we have initialized a class called “Temperature” in the IComparable interface. In the main method of this program, we created a variable and assigned it random values. Two entities present to alter the values according to the universal conversion and provide a unique result. We will go through some checks using the CompareTo() function, compare the values with these checks, and sort them in ascending order, which will be printed on the output screen using the foreach loop.

The execution of the previous code will give the following output:

All randomly generated integers have passed all the criteria provided in the IComparable interface and the CompareTo() method, as they are all sorted in ascending order in the output.

Example 04: Using the CompareTo() Method To Sort Class Objects in Ubuntu 20.04

In this example, we will compare instances from the same class and sort them by Model Year in ascending order. The Sort() function will be utilized with the CompareTo() method in this example. We will display both the sorted and unsorted lists of Employees before and after the comparison to prevent losing the logic of the IComparable interface. The following code was created in the Ubuntu 20.04 environment and will be executed using the GCC compiler on the command line terminal.

Text Description automatically generated

Text Description automatically generated

In the previous code, we’ve formed a class called “Car.” Then, we created several class objects with values, such as car name and model year. Then, using the CompareTo() function, we compared and sorted the objects we built previously in the IComparable interface. Following that, we used the Sort() method with the name of the Car class’s object and displayed both the sorted and unsorted lists of the class’s objects on the output screen to demonstrate the difference. We utilize a foreach loop to go through the list of items one by one to display these lists.

Now, we will compile and execute the previous code to get the following output:

Text Description automatically generated

In the previous output, there are two types of lists presented. In the first list, we have an unsorted representation. While in the second list, the objects are sorted concerning the Model Year of the car.

Conclusion

This article discussed the Icomparable interface in the C sharp programming language. We also discussed a comparison function present in this interface, called compare two functions. This function compares multiple objects of a similar class and can perform several checks for the object to be modified and sorted. After this, we implemented several examples of this phenomenon in the Ubuntu 20.04 environment.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.