-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do we always need to make a separate class that implements Comparator<> (e.g. in lecture 5 slide 14)? #34
Comments
For this lab I think it is necessary, the class needs to implement the comparator interface which contains the containsTo method. |
Though I used compareTo but I did not use the sort method in ImList but instead I used set method in imList to swap elements in the list. |
I am assuming you're talking about this lab Sorry if I'm wrong, but I don't think you need that since we are talking about Comparable (implemented by Booking class), and Comparable is different from Comparator. So try not to use the ImList sort, and better do swap elements using set method (whereas the checker for the swap is the compareTo method you created inside Booking class) |
I think its the easier way to solve level 5 if you are referring to lab 4. |
if I understand your question correctly, yes you can make a Comparator as a separate class. Building upon what @wk1267 mentioned, if you don't wish to construct a new class you can try googling how to use lambda functions in java |
I think if you want to use the sort method for ImList, then you need to have the comparator to pass into sort(...) so that it will know how to sort. If you don't want to use sort then maybe it's not necessary ... |
For lab4, creating a new class for the comparator method would be much easier. U can also utilise the compareTo that was implemented in level 4 to in the comparator method to help u to do the sorting |
What I did was to use a lambda to fill the cmp argument, as typing a whole additional comparator class is a pain haha. If you are interested, can take a look at: |
If you dont want to use a lambda functions or create a new class for ImList.sort(), you can use Comparator's naturalOrder(). It takes in no arguments and you just have to import Comparator to use it. The only thing that is required is that the objects that the ImList stores must implement Comparable correctly. An example: The opposite of naturalOrder() is reverseOrder() which sorts from highest to lowest and is used in the same way. |
For Lab 4, it makes Level 5 a lot easier. By implementing a class which |
No description provided.
The text was updated successfully, but these errors were encountered: