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));
}
4
  • 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. Commented Aug 19, 2019 at 16:01
  • @MK. I edited to show how I first tried sorting, with them as integer. Commented Aug 19, 2019 at 16:03
  • Write an entire example (MCVE), showing us how you create your initial list or array. Commented Aug 19, 2019 at 16:05
  • your code as written should work and I tried it and it seems to work.repl.it/repls/DesertedCrazySites Commented Aug 19, 2019 at 22:40

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that the first statement will also work for an int[], because Integer provides valueOf that takes int.
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!
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]

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.