What are the different ways of performing sorting in java -
this question asked in interview . except collections.sort() other methods .
sorting interfaces
comparable<t>
, comparator<t>
standard interfaces used standard java sorting options (all except on primitive arrays, is).
in java-speak, class implements comparable has "natural order". (technically, must implement comparable<? super e>
e matches itself, have natural order). classes don't have natural order can sorted using external comparator. comparator can used implement sort order runs against natural order (e.g. sort strings in descending alphabetic order).
these principles applied both in sortedset<e>
, sortedmap<k,v>
implementations, sort elements / keys automatically using either natural order or supplied comparator.
these same principles can found in static utility methods
arrays.sort(arr)
,arrays.sort(arr, comparator)
arrayscollections.sort(list)
,collections.sort(list, comparator)
lists
that can used sort arrays , lists, respectively, each of don't support sorting themselves.
guava
the guava library takes bit further providing abstract ordering
class base implementation of comparator
contains standard methods providing reverse view of , many convenience methods sorting , retrieving based on ordering
.
reference
a article object ordering can found in sun java tutorial's collection trail.
Comments
Post a Comment