简体   繁体   中英

how to sort numerical arraylist in ascending order

I have an ArrayList which I have sorted using Collections.sort . But the outcome looks like down below. How do I get it to be ascending order?

[0, 1, 11, 12, 12, 13, 13, 14, 14, 16, 16, 16, 17, 17, 17, 18, 18, 19, 2, 2, 20, 20, 20, 21, 21, 22, 4, 7, 7, 7, 8, 9, 9, 9, 9, 9]

Collections.sort method works fine if you don't mixed numeric and string types;
Here's an example:

       import java.util.*;
       public class ArrayListOfIntegers  {

        public static void main(String args[]){
        ArrayList<Integer> arraylist = new ArrayList<>();
        arraylist.add(12);
        arraylist.add(0);
        arraylist.add(12);
        arraylist.add(1);
        arraylist.add(12);
        arraylist.add(11);
       /* ArrayList before the sorting*/
        System.out.println("Before Sorting:");
        for(int counter: arraylist){
             System.out.println(counter);
         }

        /* Sorting of arraylist using Collections.sort*/
           Collections.sort(arraylist);

        /* ArrayList after sorting*/
         System.out.println("After Sorting:");
         for(int counter: arraylist){
             System.out.println(counter);
         }
      }
   }

Output : 在此处输入图片说明

Good luck;

Java 8:

List<String> numbers = new ArrayList<>();
    // add numbers...
    Collections.sort(numbers, Comparator.comparing(Integer::valueOf));

Looks like your ArrayList contains strings instead of numbers. You can sort it in numerical order by passing a custom Comparator to the sort() method.

In java 8, you can use Comparator.comparingInt() to do the actual comparision, in combination with the Integer.parseInt() method to perform the conversion from string to int:

ArrayList<String> list = new ArrayList<>();
// ... add values    

list.sort(Comparator.comparingInt(Integer::parseInt));

Java7: Collections.sort() methods works differently for String and Integer data types.
I assume that you have ArrayList<String> . One solution is to convert it to ArrayList<Integer> and then pass through Collections.sort()

    ArrayList<String> strList = new ArrayList<String>();
    strList.add("0");
    strList.add("1");
    strList.add("11");
    strList.add("12");
    strList.add("2");
    strList.add("02");
    strList.add("004");
    strList.add("9");
    strList.add("20");

    System.out.println("strList Unsorted :" + strList);
    Collections.sort(strList);
    System.out.println("strList Sorted :" + strList);

    //Convert ArrayList<String> to ArrayList<Integer>
    List<Integer> intList = new ArrayList<Integer>(strList.size());
    for (String strValue : strList) {
        intList.add(Integer.parseInt(strValue));
    }

    System.out.println("intList Unsorted :" + intList);
    Collections.sort(intList);
    System.out.println("intList Sorted :" + intList);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM