Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
IBM : developerWorks : Java : Education - online courses
Java Collections Framework
Download tutorial zip fileView letter-sized PDF fileView A4-sized PDF fileE-mail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPreviousNext
6. Algorithm support
  


Sorting arrays page 2 of 6


While the TreeSet and TreeMap classes offer sorted version of sets and maps, there is no sorted List collection implementation. Also, prior to the collections framework, there was no built-in support for sorting arrays. As part of the framework, you get both support for sorting a List, as well as support for sorting arrays of anything, including primitives. With any kind of sorting, all items must be comparable to each other (mutually comparable). If they are not, a ClassCastException will be thrown.

Sorting of a List is done with one of two sort() methods in the Collections class. If the element type implements Comparable then you would use the sort(List list) version. Otherwise, you would need to provide a Comparator and use sort(List list, Comparator comparator). Both versions are destructive to the List and guarantee O(n log2n) performance (or better), including when sorting a LinkedList, using a merge sort variation.

Sorting of arrays is done with one of eighteen different methods. There are two methods for sorting each of the seven primitive types (except boolean), one for sorting the whole array and one for sorting part of the array. The remaining four are for sorting object arrays (Object[ ]).

To sort primitive arrays, simply call Arrays.sort() with your array as the argument and let the compiler determine which of the following methods to pick:

  • void sort(byte array[ ])
  • void sort(byte array[ ], int fromIndex, int toIndex)
  • void sort(short array[ ])
  • void sort(short array[ ], int fromIndex, int toIndex)
  • void sort(int array[ ])
  • void sort(int array[ ], int fromIndex, int toIndex)
  • void sort(long array[ ])
  • void sort(long array[ ], int fromIndex, int toIndex)
  • void sort(float array[ ])
  • void sort(float array[ ], int fromIndex, int toIndex)
  • void sort(double array[ ])
  • void sort(double array[ ], int fromIndex, int toIndex)
  • void sort(char array[ ])
  • void sort(char array[ ], int fromIndex, int toIndex)

The sorting of object arrays is a little more involved, as the compiler doesn't check everything for you. If the object in the array implements Comparable, then you can just sort the array directly, in whole or in part. Otherwise, you must provide a Comparator to do the sorting for you. You can also provide a Comparator implementation if you don't like the default ordering.

  • void sort(Object array[ ])
  • void sort(Object array[ ], int fromIndex, int toIndex)
  • void sort(Object array[ ], Comparator comparator)
  • void sort(Object array[ ], int fromIndex, int toIndex, Comparator comparator)

Main menuSection menuGive feedback on this tutorialPreviousNext
PrivacyLegalContact