0

I have a String list of numbers (they can be an int list, just need to turn it into an array list to return), this numbers can be from a range, for example from 1 to 25.

I tried sorting them using Collections.sort(numbersList); but it sorts them in a weird way. For example this is the current sorting:

1
10
11
..
2
20
21
..

What I really want is for it to be sorted in a numerical order, for example:

1
2
3
..
10
11
..

Tried to sort them as an int[] with Arrays.sort(numbers); but it gives the same result.

Here is the code that I use to generate the numbers and turn them into an array list.

int[] range = IntStream.rangeClosed(1, 30).toArray();
Arrays.sort(range);
List<String> rangeList = new ArrayList<>();
for (int number : range) {
    rangeList.add(String.valueOf(number));
}
user3745394
  • 37
  • 1
  • 8
  • 2
    When you sort them as strings, it will give you an alphanumeric ordering in which "10" goes before "2". Sorting ints should work, you probably didn't do it correctly, but you didn't show your code so we can't help you. – MK. Aug 19 '19 at 16:01
  • @MK. I edited to show how I first tried sorting, with them as integer. – user3745394 Aug 19 '19 at 16:03
  • Write an entire example ([MCVE](https://stackoverflow.com/help/mcve)), showing us how you create your initial list or array. – Basil Bourque Aug 19 '19 at 16:05
  • your code as written should work and I tried it and it seems to work.https://repl.it/repls/DesertedCrazySites – MK. Aug 19 '19 at 22:40

3 Answers3

5

I feel like I've seen this before, but you can try the following in Java 8+ (where numbersList is the String[]):

Arrays.sort(numbersList, Comparator.comparing(Integer::valueOf));

Additionally, in your code, you have a problem in your initialization of range, because the parenthesis for Integer.parseInt is not closed, and the parameters are wrong. It should be replaced with the following:

int[] range = IntStream.rangeClosed(1, 30).toArray();

You won't need to sort after doing this, because IntStream.rangeClosed promises that all the elements from the IntStream will be provided in sorted order. If you'd like more information on how to properly use Integer.parseInt, I'd highly recommend checking out the Javadoc.

Avi
  • 2,611
  • 1
  • 14
  • 26
  • Note that the first statement will also work for an `int[]`, because Integer provides [valueOf that takes int](https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#valueOf(int)). – Avi Aug 19 '19 at 16:14
  • Yeah I just noticed, in the code there is parseInt correctly, for a better example i tried to remove them, but forgot the first one. This worked though! – user3745394 Aug 19 '19 at 16:54
1

If you have a list of strings, you need to compare them as integers to sort. In Java 8+ you can do following:

listOfStrings.sort( Comparator.comparing( Integer::valueOf ) );

Example :

List< String > strings = Arrays.asList( "1","3","0","21","4","2","3");
strings .sort( Comparator.comparing( Integer::valueOf ) );
System.out.println( strings );
//prints [0, 1, 2, 3, 3, 4, 21]
stackFan
  • 1,528
  • 15
  • 22
0

A more efficient alternative is sticking to compare ints (primitives) rather than Integers (objects) :

Arrays.sort(numbers, Comparator.comparingInt(Integer::parseInt));

davidxxx
  • 125,838
  • 23
  • 214
  • 215