-1

I want to write a generic comparator class which will sort my ArrayList. In order to do so, what should I exactly write inside my compare function of static class ElementComparator?

 public static void main(String[] args) throws InterruptedException {
    Scanner sc = new Scanner(System.in);

    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(-2);
    list.add(-1);
    list.add(10);

    Collections.sort(list,new ElementComparator());

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }

}
static class ElementComparator <T extends Comparable<T>> implements Comparator <T> {


    @Override
    public int compare(T o1,  T o2) {

    }   
};
  • https://stackoverflow.com/questions/12397267/java-a-single-generic-method-to-reverse-arraylist-or-list-of-objects – Mitul Gedeeya Jan 06 '18 at 03:26
  • 1
    Do you want to *reverse* or to *sort*? Perhaps to sort *descending*, aka in reverse order? They are entirely different things. [`Collections.reverse(List)`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#reverse-java.util.List-) vs [`Collections.sort(List, Comparator)`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-) using [`Collections.reverseOrder()`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#reverseOrder--) as the comparator. – Andreas Jan 06 '18 at 03:54
  • Do you want to **reverse** the order of the elements, or **sort them in descending order**? They are completely different things, but your question is unclear as to what you want. Please edit your question to make clear *exactly* what you want to do. – Bohemian Jan 06 '18 at 04:41
  • edited . @Bohemian – Munificient Jan 06 '18 at 04:47
  • Still unclear: *"which will sort/reverse my list"*... which is it? Sort or reverse? – Bohemian Jan 06 '18 at 06:02
  • @Bohemian edited – Munificient Jan 12 '18 at 14:20
  • @Munificent Still unclear. The problem is this: *sort/reverse*. sorting and reversing are very different and actually unrelated tasks. Please edit your question to chose **ONE** of these terms: **Either** "sort" **or** "reverse". Not *both* – Bohemian Jan 12 '18 at 15:07
  • corrected @Bohemian – Munificient Jan 14 '18 at 05:56
  • @Munificent Corrected? The first sentence of your question is still *I want to write a generic comparator class which will **sort/reverse** my ArrayList*. Please remove "sort/reverse" from it and replace with either "sort" or "reverse" – Bohemian Jan 14 '18 at 10:38
  • @Bohemian Sorry, I made you bored multiple times. I look at only heading always. Sorry for that – Munificient Jan 14 '18 at 14:59

1 Answers1

0

Your generic type has a common base class - Comparable, therefore you can directly call the compareTo method on the objects and they should invoke that method to get the comparison result.

@Override
public int compare(T o1,  T o2) {
    return o2.compareTo(o1);
} 
smac89
  • 39,374
  • 15
  • 132
  • 179
  • And what would T2 represent ? – whatamidoingwithmylife Jan 06 '18 at 03:29
  • @GCP, just copied verbatim what OP posted. That's a typo – smac89 Jan 06 '18 at 03:30
  • 1
    He seems to be saying he wants to *reverse* sort the elements. In that case, I would think you want to use `o2.compareTo(o1)`. – John Bollinger Jan 06 '18 at 03:30
  • @JohnBollinger yes , you are right. Sollution works. Thanks all – Munificient Jan 06 '18 at 03:33
  • @GCP that was a typing mistake at the time of writing the question :) – Munificient Jan 06 '18 at 03:35
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/350567) by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – iBug Jan 06 '18 at 04:35