Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Comparer in C#

In this article, we are going to learn about the Comparer Class in C#.<p> <h3>Need for Comparer Class</h3> <p>For primitive data types, it is easy to compare two objects or variables.
For example, two integers can be easily compared using their numerical value. Two Strings can be compared according to their lexicographical order.
But what about objects of classes which consists of different data types? How would the compiler know if one object is equal to another object? What if we need to sort a list of objects? How would we define the comparison parameter for two objects?

This is where the Comparer class comes in handy.
Comparer is an object-to-object comparer that allows us to compare objects recursively member by member and to define custom comparison rules for certain properties, fields, or types. Objects Comparer can be considered as a ready-to-use framework or as a starting point for similar solutions.


Example:

Let us consider a simple class as follows:
<pre>class Point { public int x; public int y; public Point(int x, int y){ this.x = x; this.y = y; } } </pre> This is a simple class which stores the co-ordinates of a point on the cartesian plane.

If we have a list of Point, List < Point > list_name = new List < Point >(); And we need to sort it according to the x - coordinate (ascending), and if two points have the same x - coordinate, we compare the y - coordinate. If we simply use list_name.Sort();, it would throw an error. So, we need to define a compare for the objects of the class Point. <pre> public class ComparePoint : Comparer < Point > { public override int Compare(Point A, Point B) { if (A.x != B.x) { return A.x - B.x; } else { return A.y - B.y; } } } </pre> We created a class ComparePoint which implements Comparer. Here, we override the Compare() function, which checks for the equality of two objects and define our own conditions. Now, if we need to sort the list of Point:
list_name.Sort(new ComparePoint());
We just need to pass an instance of the ComparePoint class as a parameter to the Sort() function. Now, our list is sorted according to the conditions defined above.


Task

A class Cube is defined for you. Define two comparers in the code editor.

  • CompareDimension: It sorts ascending for the length first. If the lengths are equal, sort descending for breadth. If both are equal, sort ascending for height.
  • CompareVolume: It sorts ascending according to the volume of the cube.
Start solving Comparer in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test