O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sorting a Collection

// sorting an array
											int[] myInts = {1,5,7,8,2,3};
											Arrays.sort(myInts);
											// sorting a List
											List myList = new ArrayList();
											myList.put(obj1);
											myList.put(obj2);
											Collections.sort(myList);

The Arrays class is a class in the java.util package that contains a bunch of static methods for manipulating arrays. The useful method here is the sort() method. The sort() method takes an array of objects or primitives along with optional from and to indexes. The from index, if passed, would specify the index of the first element to be sorted, and the to index would specify the index of the last element to be sorted. Primitives are sorted in ascending order. When using this method to sort objects, all the objects must implement the Comparable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required