using comparator for sorting in java

0 votes

I know how to use the comparable but having difficulty with the Comparator, please explain using an example

Jun 5, 2018 in Java by sharth
• 3,370 points
597 views

1 answer to this question.

0 votes

You've got two problems:

1) You're using Collections.sort (which takes a List<E>), but trying to sort an array. Use Arrays.sort instead.

2) You need to specify that you're implementing Comparator<Software>, not just the raw Comparator type.

So basically, this works:

Arrays.sort(aSoftware, new Comparator<Software>() {
    public int compare(Software s1, Software s2) {
        return s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle());
    }
});
answered Jul 6, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
2 answers

Give an example for encryption and decryption in AES using Java

getfullstring: Splashscreen.text >>> READ MORE

answered May 8, 2019 in Java by anonymous
6,555 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
661 views
+1 vote
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,620 points
4,441 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 5, 2018 in Java by geek.erkami
• 2,680 points
1,922 views
0 votes
1 answer

When to use LinkedList over ArrayList in Java?

LinkedList and ArrayList are two different implementations of the List ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,910 points
1,647 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
4,789 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,910 points
776 views
0 votes
2 answers
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,527 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP