views:

176

answers:

5

This question was asked in an interview . Except Collections.sort() what are the other methods .

+1  A: 

Arrays.sort(..), or implement sorting yourself.

Bozho
A: 
  1. Insertion Sort is used for small size arrays.
  2. Dual Pivot Quick Sort is used for larger size arrays.
  3. MergeSort is used for sorting object.
Suresh S
Also, you can use a TreeMap to sort via a Red-Black Tree.
daitangio
A: 

You can stick your data into a SortedSet (e.g. TreeSet) and pull it out again in sorted order by traversing with an iterator. This is not really a general sorting method (since it doesn't allow duplicated entries), but would work for quite a lot of data set.

disown
+7  A: 

Comparable and Comparator are the standard interfaces used by all standard Java sorting options (all except those on primitive arrays, that is).

In Java-speak, any Class that implements Comparable has a "natural order". (Technically, it must implement Comparable<? super E> where E matches itself, to have a natural order). Classes that don't have a natural order can be sorted using an external Comparator. A Comparator can also be used to implement a sort order that runs against the natural order (e.g. sort Strings in descending alphabetic order).

These principles are applied both in the SortedSet and SortedMap implementations, that sort their elements / keys automatically using either the natural order or a supplied comparator.

These same principles can also be found in the static Utility methods

that can be used to sort arrays and lists, respectively, each of which don't support sorting themselves.


The Guava Library takes this a bit further by providing the abstract Ordering class as base implementation of Comparator which already contains standard methods like providing a reverse view of itself and many convenience methods for sorting and retrieving based on this Ordering.


Reference

A good article about Object Ordering can be found in the Sun Java Tutorial's Collection Trail.

seanizer
+4  A: 
  1. SortedSet or SortedMap, provided the keys are unique.
  2. PriorityQueue.
  3. Arrays.sort().
  4. Own code.
  5. A JDBC query with an ORDER BY clause.
  6. An LDAP search against an indexed attribute.

I'm sure there are more.

EJP
This is actually an interesting answer (+1), but I am missing explanatory sentences or at least reference links. The question whether JDBC sorting happens in java is an interesting one, for example.
seanizer
JDBC doesn't sort. JDBC tells the database to sort. Nothing much to discuss there. Ditto for the LDAP answer. I don't know why you think you need links to Java classes. You didn't ask for links, or explanatory sentences either actually: if you want the latter you are welcome to *ask* but I don't accept that they are just 'missing'.
EJP
Do you think that attitude is helpful? A good developer should not only know his code but also be able to communicate what it does.
seanizer
I'm not crazy about your attitude either. I answered your question, so you start in telling me what's missing, that you didn't ask for originally, and that in my opinion is either obvious or well documented elsewhere. As I didn't provide any code your final sentence lacks relevance.
EJP