#Where can I use a comparator (collections)?

93 messages · Page 1 of 1 (latest)

barren steppe
#

Hi,
I am looking into using a comparator when creating a TreeMap currently. Where else can you use comparators to sort Objects?

neat ermineBOT
#

This post has been reserved for your question.

Hey @barren steppe! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

weak bison
#

PriorityQueue and TreeSet? im not quite sure that's what you're asking

gusty glen
#

list.sort() too. stream().sort()

gusty glen
#

kinda redundant with List.sort(), no?

autumn bolt
#

java lang developers decided to have it in Collections class, so maybe not

#

the fun thing, both methods have this: ```java
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
...

#

both use Arrays.sort()

neat ermineBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

barren steppe
#
@Override    
public int compare(String wordA, String wordB) {        
if (wordA.length() < wordB.length()){            
return 1;        
} else if (wordA.length() == wordB.length()){            
return 1;        
} else {            
return -1;        
}    
}});```
weak bison
#

you don't really need all that

#

you could just use Comparator.comparingByInt(String::length)

barren steppe
#

I feel like we arent allowed here bacause we havent learned yet what :: means

#

But, where can I find the docs to learn about the Comparator, I need something similar for another task. We get a List<> of Objects and shall sort them via their returned time values from their time() methods

weak bison
barren steppe
weak bison
#

if not you can also ust use wordA.length() - wordB.length() instead of your entire condition tree there

weak bison
barren steppe
weak bison
#

oh your compare isn't stable, you need a return 0; for the second condition there

barren steppe
barren steppe
#

The operation returns minus something if you have MAX_VALUE and -1

weak bison
barren steppe
weak bison
barren steppe
#

If I have string "abc" than "cde" will never be added to the list. Order doesnt matter when we have the same length so I just used 1 instead of 0

barren steppe
neat ermineBOT
# barren steppe Ohhh ok. thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

weak bison
#

only comparing by length

barren steppe
#

Just adding random words

weak bison
#

yeah. it's considering strings of equal length as equal so it doesn't add it to the set

#

but having it be 1 makes your sort unstable

barren steppe
weak bison
#

you need to have a definite sorting order for TreeSet

#

you could just make it sort alphabetically next. the equality check would be accurate there

barren steppe
#

Well, the task says order doesnt matter when the length is equal but the words just have to be there

weak bison
#

having an unstable sort isn't exactly a good way to do that.

barren steppe
#

So

[iernrigfnekfnerj, Meatlable, Zebra, All, No, OK]

could also be

[iernrigfnekfnerj, Meatlable, Zebra, All, OK, No]

#

||Dude codeblocks suck on Discord||

weak bison
#

||skill issue||

#

see that's an unstable sort. TreeSet, and other sorting stuff, don't like that. TreeSet just treats equality as exclusionary, since, yknow, it's a set.

barren steppe
#

||I am adding three ` prior to and after the text||

weak bison
#

||looks like 2 in your message||

#

||you could also just make it inline with single backticks||

barren steppe
#

||It deletes them for no reason, LITERALLY||

weak bison
#
[iernrigfnekfnerj, Meatlable, Zebra, All, No, OK]
[iernrigfnekfnerj, Meatlable, Zebra, All, OK, No] 
barren steppe
weak bison
#

this is a cool way to hold simultaneous conversations.

barren steppe
#

So can I use Comparators with array lists?

weak bison
#

via one-time sorts, yeah. but you can't make it hold the sorting.

#

you can use a PriorityQueue for pop or dequeue in the desired order, but iteration doesn't give a sorted order

barren steppe
weak bison
#

that's unanswerable

#

List is just an interface

#

well, sort exists on that interface

#

but beyond that, you need to ask about a concrete implementation

barren steppe
#

Yep, but the task literally says that I get a "list of results"

weak bison
#

list doesn't always mean List

barren steppe
#

: /

weak bison
#

list in human terms is just a sequence of elements

#

if it doesn't say to use List explicitly, then it probably isn't that

barren steppe
#

Result is a class here btw

weak bison
#

but if it's just the result that matters, you could use some implementation of List and sort it before yielding the result

barren steppe
#

I get a List of "Result" objects of which every has the public methods String name and Time time. I have to sort ascending in time. It sounds like another comparator to me, right?

#

I found the given code, its literally a List<Result> results-list

weak bison
#

k

#

that clears that up

weak bison
barren steppe
#

I am a bit annoyed that I cannot test the thing right away because they didnt even add the list

#

Wait why did they add a compareTo method in the Time class? That makes no sense to me

weak bison
#

why shouldn't you be able to compare times

neat ermineBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

barren steppe
#

Now I have to use a compareTo method in compare in the comparator

weak bison
weak bison
#

you just.. don't

#

if a class implements Comparable, then it's considered to have a natural ordering, and it contains its own Comparator
if you just omit the Comparator in the construction, or use Comparator.naturalOrder(), you can use that natural ordering

#

Comparator.reverseOrder() also exists to reverse the natural ordering

#

String, Integer, Long, etc also implement Comparable.

barren steppe
weak bison
#

List isn't sorted, as i said in your other thread

#

you have to sort it first