sorting integers in order lowest to highest java

后端 未结 7 1269
刺人心
刺人心 2020-11-30 13:03

These numbers are stored in the same integer variable. How would I go about sorting the integers in order lowest to highest?

11367
11358
11421
11530
11491
11218
11         


        
7条回答
  •  眼角桃花
    2020-11-30 13:42

    There are two options, really:

    1. Use standard collections, as explained by Shakedown
    2. Use Arrays.sort

    E.g.,

    int[] ints = {11367, 11358, 11421, 11530, 11491, 11218, 11789};
    Arrays.sort(ints);
    System.out.println(Arrays.asList(ints));
    

    That of course assumes that you already have your integers as an array. If you need to parse those first, look for String.split and Integer.parseInt.

提交回复
热议问题