Sort Integer in Ascending Order in Java Example



java.uti.Collections Examples


In this example, we will take Integer wrapper class and sort it in ascending order by using Collections.sort() method.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortWrappersExample {
 
    public static void main(String[] args) {
        sortWrapperClassObjectsInAsc();
    }
    private static void sortWrapperClassObjectsInAsc() {
        List<Integer> names = new ArrayList<>();
        names.add(100);
        names.add(20);
        names.add(10);
        names.add(50);

        System.out.println("Before Sorting : " + names);
        Collections.sort(names);
        System.out.println("After Sorting : " + names);
    }
}

Output

Before Sorting : [100, 20, 10, 50]
After Sorting : [10, 20, 50, 100]

Comments