Click here to Skip to main content
11,703,632 members (67,358 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java
Sorting Method in java which use to sort any Object pass as parameter and return sorted Object
Posted 30-Apr-13 0:11am
Prasad Khandekar at 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.

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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
0 OriginalGriff 550
1 Maciej Los 530
2 Sergey Alexandrovich Kryukov 381
3 Andy Lanng 294
4 CPallini 195
0 OriginalGriff 8,744
1 Sergey Alexandrovich Kryukov 7,747
2 CPallini 5,107
3 Maciej Los 4,716
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100