From the course: C# Practice: Interfaces and Abstract Classes

Solution: Make types sortable with IComparable - C# Tutorial

From the course: C# Practice: Interfaces and Abstract Classes

Solution: Make types sortable with IComparable

- [Speaker] The goal for this code challenge was to make the temperature class sortable by implementing the Icomparable of T comparedTo method. Now you can see here in my class that I have done that, I've implemented Icomparable of T. And in this case, T is temperature. And that means that when I create this compareTo method, it's strongly typed or the parameter that's passed in is strongly typed as a temperature. Now before we talk about the code that's in here, let's look at the overall code for the temperature class. The underlying store for the temperature is here. It's a double value. It's called _kelvin value. And when you set the temperature like I'm doing here in the test code, we're here to 400 that's being passed in and the _kelvin value is being set to 400. Now if I want to retrieve that value there's a property called kelvin that just retrieve that value. There's also one for Fahrenheit that retrieves that value and then applies an expression to it to turn that into kelvin, into Fahrenheit units I should say. And then the same in Celsius that changes the Kelvin into Celsius units. The best value to sort on is the _kelvin value. Also, I put some notes in here. I'm implementing the Icomparable compareTo method. But this class should also define the is greater than operator and the is less than operator and some of these other operators. Okay, so now let's go up here and look at compareTo. It's going to get a instance of a temperature. So the way this is going to get called is it's going to call two temperature objects. It's going to compare the current one to another temperature object. And our job is to return int. We return a positive number if the current temperature is greater than the other temperature. We return a negative number if it's less than and we return zero if they're equal. So the first step in my code is to check to see whether this is null. If this is null, then we return one because we assume that the current temperature is greater than null. Now the next question is, how do I write the code to return one, negative one or zero? And of course I've said in the instructions they don't have to be one. It can be any positive number. Same, it could be any negative number but most code or most implementations of comparedTo just return one, negative one or zero. So, my first attempt at this is just to check if the current Kelvin is larger than the other Kelvin, return one. If it's less than, return negative one. And if it's anything else, return zero. But here's another way I could have solved this is if you look at the Kelvin, it's a double. And in the .net framework, the double type already supports comparedTo. So, if I were to comment at these three lines or four lines or how many lines that is, control K, control C, come down here and uncomment this, I'm saying look at the _kelvin value and compare that to the other temperatures _kelvin value. And since they're both doubles and doubles supports the compareTo method, this should work as well. And we have success. So that shows one or I should say it shows two different ways of writing code for in the comparedTo method.

Contents