GithubHelp home page GithubHelp logo

Comments (10)

daitenshionyan avatar daitenshionyan commented on August 11, 2024 1

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.

from 2122-s2.

wk1267 avatar wk1267 commented on August 11, 2024

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

from 2122-s2.

tanweiming00 avatar tanweiming00 commented on August 11, 2024

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.

from 2122-s2.

calvinseptyanto avatar calvinseptyanto commented on August 11, 2024

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)

from 2122-s2.

gordengorden avatar gordengorden commented on August 11, 2024

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

from 2122-s2.

adityajirafe avatar adityajirafe commented on August 11, 2024

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

from 2122-s2.

Ohnojiso avatar Ohnojiso commented on August 11, 2024

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

from 2122-s2.

Brianliew77 avatar Brianliew77 commented on August 11, 2024

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

from 2122-s2.

shotnothing avatar shotnothing commented on August 11, 2024

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

from 2122-s2.

undeser avatar undeser commented on August 11, 2024

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.

from 2122-s2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.