Solved Descending order

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 25, 2016.

  1. Hello so lets say I have this integer list

    Code (Java):
    Numbers:
    - 10
    - 2
    - 12
    - 5
    how do I print out these numbers into descending order ?
    such as

    Code (Java):
    printout(12);
    printout(10);
    .....
     
  2. @Ferdz

    got in trouble

    when I did

    Code (Java):
            List<Integer> list = new ArrayList<Integer>();
            list.add(5);
            list.add(6);
            list.add(10);
            list.add(20);
         
            Collections.sort(list);
            Collections.reverse(list);
         
            for(Integer i : list) {
                System.out.println(i);
            }
    its printing like this

    Code (Java):
    -20
    20
    10
    6
    5
     
    #4 MadMaxCookie, May 25, 2016
    Last edited: May 25, 2016
  3. Collections.reverse obviously wont sort a list for you, you have to sort it first and then reverse it ;)
     
  4. yes sorry I didn't add it on my post but I did it already
    same print out
     
  5. MiniDigger

    Supporter

    where does the -20 even come from? you never added it to the list in the code snipped you gave us :)
    Collections.sort uses mergesort (yeah, I know that that is not entirely true) to sort the list in something that is called the "natural order". It uses the compareable interface to figure out that order. I don't see why this should not work for integers < 0.
     
  6. Edit : hell yeah , Sorry I have another int that print out in that class.
     
    • Funny Funny x 1