-2

Is it possible to compare Integers in the compareTo() method in conjunction with Collections.sort() because when I run the following code it will not compile; the compiler says there is an error with this as it compares this to the other integer.I am trying to sort the numbers in descending order.

import java.util.*;

public class main implements Comparable <Integer> {
  public static void main(String [] args) {
    ArrayList <Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(5);
    list.add(4);
    Collections.sort(list);
    System.out.println(list);
  }


  public int compareTo(Integer other) {
    if (this > other){
      return -1;
    }

    if (this < other){
      return 1;
    }

    return 0;
  }
}
Rohan
  • 541
  • 1
  • 11
  • 24
  • 5
    `this` makes no sense. – shmosel Mar 13 '18 at 22:29
  • Possible duplicate of [How to implement the Java comparable interface?](https://stackoverflow.com/questions/21626439/how-to-implement-the-java-comparable-interface) – Ousmane D. Mar 13 '18 at 22:30
  • 2
    Remove the `compareTo` method and the `implements Comparable`. You don't need them, since `Integer` is already `Comparable`. – Andreas Mar 13 '18 at 22:32
  • https://stackoverflow.com/questions/5894818/how-to-sort-arraylistlong-in-java-in-decreasing-order – Ousmane D. Mar 13 '18 at 22:40

4 Answers4

1

this is an instance of main, you can't apply the < or > operators to it. And frankly, you don't need to. Just use Integer's natural ordering and revere it:

list.sort(Comparator.<Integer>naturalOrder().reversed());
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Making your own Comparable<Integer> isn't going to change the default ordering of Integer. Integer is final, so you can't sub-class it. You could write a Comparator<Integer> and use that. Or, you could simply get the reverseOrder() Comparator. Like,

Collections.sort(list, Comparator.reverseOrder());

or

Collections.sort(list, (a, b) -> Integer.compare(b, a));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Jack <J@ck>
 * https://stackoverflow.com/questions/49266832/integer-in-compareto
 */
public class ListReverse {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);

        // Creates new list wit reverse order
        List<Integer> reverseList = list.stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
        System.out.println(reverseList);
    }
}
Jackkobec
  • 5,889
  • 34
  • 34
0

It's always better to use Collections.sort(list, c) to sort an ArrayList

If you want to do it yourself, here is an example of bubble-sort

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(5);
        list.add(4);
        Main.bubble(list);
        System.out.println(list);
    }

    private static void swap(ArrayList<Integer> list, int x, int y) {
        Integer temp = list.get(x);
        list.set(x, list.get(y));
        list.set(y, temp);
    }

    public static void bubble(ArrayList<Integer> list) {
        boolean done = false;

        while (!done) {
            done = true;
            for (int i = 0; i + 1 < list.size(); i++) {

                /* Sort */
                if (list.get(i) > list.get(i + 1)) {
                    swap(list, i, i + 1);
                    done = false;
                }

//                /* Sort - in reverse order */
//                if (list.get(i) < list.get(i + 1)) {
//                    swap(list, i, i + 1);
//                    done = false;
//                }
            }
        }
    }
}