Click here to Skip to main content
14,880,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorting Method in java which use to sort any Object pass as parameter and return sorted Object
Posted
Comments
Prasad Khandekar 30-Apr-13 6:25am
   
Please have a look at the documentation for java.util.Arrays#sort(Object[] a). This utility method sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface.

Regards,
Maciej Los 30-Apr-13 14:33pm
   
Can't you use generics to sort, compare "objects"?

1 solution

You cannot 'sort any object'.
Strictly speaking you may just sort sets having an order criterion defined.
If you need sorting a collection (e.g. an array) of objects then the latter must provide a property (say 'key') such that using it you are be able establish the relative order of any two of them.
Supposing the objects provide such a key then you may use a well-known-good algorithm (like for instance Quick Sort[^] ) for sorting them.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900