sorting integers in order lowest to highest java

后端 未结 7 1151
刺人心
刺人心 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:36

    You can put them into a list and then sort them using their natural ordering, like so:

    final List list = Arrays.asList(11367, 11358, 11421, 11530, 11491, 11218, 11789);
    Collections.sort( list );
    // Use the sorted list
    

    If the numbers are stored in the same variable, then you'll have to somehow put them into a List and then call sort, like so:

    final List list = new ArrayList();
    list.add( myVariable );
    // Change myVariable to another number...
    list.add( myVariable );
    // etc...
    
    Collections.sort( list );
    // Use the sorted list
    
    • Collections.sort( List )

提交回复
热议问题