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); .....
@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
Collections.reverse obviously wont sort a list for you, you have to sort it first and then reverse it
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.