Skip to content
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

Open
michael192002 opened this issue Feb 19, 2022 · 10 comments

Comments

@michael192002
Copy link

No description provided.

@wk1267
Copy link

wk1267 commented Feb 19, 2022

For this lab I think it is necessary, the class needs to implement the comparator interface which contains the containsTo method.

@tanweiming00
Copy link

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.

@calvinseptyanto
Copy link

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)

@gordengorden
Copy link

I think its the easier way to solve level 5 if you are referring to lab 4.

@adityajirafe
Copy link

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

@Ohnojiso
Copy link

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 ...

@Brianliew77
Copy link

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

@shotnothing
Copy link

What I did was to use a lambda to fill the cmp argument, as typing a whole additional comparator class is a pain haha.
Not sure if its allowed though, but checkstyle and codecrunch seem fine with it.

If you are interested, can take a look at:
https://www.baeldung.com/java-8-sort-lambda

@daitenshionyan
Copy link

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:
list = list.sort(Comparator.naturalOrder());
where list is an ImList of objects that implements Comparable (like Booking)

The opposite of naturalOrder() is reverseOrder() which sorts from highest to lowest and is used in the same way.

@undeser
Copy link

undeser commented Feb 24, 2022

For Lab 4, it makes Level 5 a lot easier. By implementing a class which implements Comparator<>, you can easily use the .sort() function from the ImList class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests