Comments (10)
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.
For this lab I think it is necessary, the class needs to implement the comparator interface which contains the containsTo method.
from 2122-s2.
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.
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.
I think its the easier way to solve level 5 if you are referring to lab 4.
from 2122-s2.
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.
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.
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.
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.
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)
- PYP Q 2d HOT 2
- PYP last qn HOT 7
- Recitation 09-extra pdf not on luminus HOT 2
- PECS HOT 5
- QN5 17/18S1 [Why is A not the answer?] HOT 4
- Q5 HOT 3
- Can anyone explain what is a functor and a monad isπ HOT 8
- Mock Q4b HOT 9
- Impending P.A.N.I.C HOT 22
- AY19/20 SS1 qn 7 HOT 4
- Question on reduce function HOT 4
- AY18/19 Q2 Cyclical Dependency HOT 3
- error in obtaining int[] from a stream HOT 4
- Q3d) When I solved the game of Life problem for labs, I used loops. Can someone post their code using streams for 3d and 3e ? HOT 2
- Assertion questions HOT 2
- Mock 4a help HOT 13
- What kind of stream operations allow us to use .parallel() operation? HOT 4
- Final Submission Folder Disappeared HOT 6
- Anyone solved the RedLight , GreenLight, AmberLight problem? Can you please post your code? HOT 2
- excercise 2 inheritence question
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2122-s2.