View RSS Feed

Advanced Java

Using Collections.sort and Comparator in Java

Rate this Entry
by , 02-18-2012 at 04:05 PM (1371 Views)
Sorting a collection in Java is easy, just use Collections.sort(Collection) to sort your values. For example:
Collection sorting is easier in Java. To sort the values, use Collection.sort (Collection). E.g

Java Code: This is the code to explain collection sort and comparator methods
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SimpleSort {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(5);
        list.add(4);
        list.add(3);
        list.add(7);
        list.add(2);
        list.add(1);
        Collections.sort(list);
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }
}

Submit "Using Collections.sort and Comparator in Java" to Facebook Submit "Using Collections.sort and Comparator in Java" to Digg Submit "Using Collections.sort and Comparator in Java" to del.icio.us Submit "Using Collections.sort and Comparator in Java" to StumbleUpon Submit "Using Collections.sort and Comparator in Java" to Google

Comments