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

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

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -