File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to Sort an Array List without using Collections.sort() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Sort an Array List without using Collections.sort() method" Watch "How to Sort an Array List without using Collections.sort() method" New topic
Author

How to Sort an Array List without using Collections.sort() method

Deepika Saxena
Ranch Hand

Joined: Jul 05, 2009
Posts: 59
Hi,
Is there any way to sort an ArrayList without using Collections.sort() method?
Please find the details.
Now I want to sort sList without using Collctions.sort(alist) or Collections.sort(alist,MyComparator).
is there any way to do this?
Any help would be highly appreciated.
--Deepika
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You could use Arrays.sort(), but I'm guessing this a homework assignment?
If so, then I suspect you'll need to implement your own sorting algorithm. There are thousands of samples across the internet.

/Dave
Deepika Saxena
Ranch Hand

Joined: Jul 05, 2009
Posts: 59
Thanks David,
If i want to implement my own algorithm, then i need to supply my own comparator too.
but, what i want to know is, is there any other way to sort an ArrayList without using Comparator as well as Collections.sort() method.

Thanks.
--Deepika
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Deepika,

the Comparator interface is just a standard way to compare "things" in the JDK which is used for example in the Collections framework. If you provide your own sorting algorithm you are free to do any kind of comparison when needed, whatever it may look like in your algorithm.

Collections.sort() is just a unified method to make all collections sortable as long as the fulfill a specific contract, i.e. being comparable or specifying an external Comparator. It would be very stupid to provide a different API for sorting for each collection in the collections framework. That's why there is this "standard" way ;-)

What problem do you have with this? Or what else are you expecting to find?

Marco
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can use either java.util.Comparator or java.lang.Comparable
Comparable provides a 'natural ordering', such as is expected with numbers - you know what their order is.
A Comparator is more useful when there could be multiple was to order (such as with students) or when you want to order on something other than the 'natural order'.

One solution would be to make the class Comparable (ie able to be compared) and then feed the items to a java.util.TreeSet. The TreeSet will use the Comparable behaviour to retain the order, and then return the items in order. This may not be what you want, but it doesn't use either Comparator or Coollections.sort()
Deepika Saxena
Ranch Hand

Joined: Jul 05, 2009
Posts: 59
Thanks David and Marco for the detail explanation.
That clears my doubt about finding a possible way to sort an ArrayList.
--Deepika.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

David O'Meara wrote:
One solution would be to make the class Comparable (ie able to be compared) and then feed the items to a java.util.TreeSet. The TreeSet will use the Comparable behaviour to retain the order, and then return the items in order. This may not be what you want, but it doesn't use either Comparator or Coollections.sort()


Agreed on the "may not be what you want" part...

If I was the professor, and told my students to not use collections.sort, or comparator/comparable in the assignment... and a student use TreeSet. I would be kinda impressed, that this student knows his/her API, and could even be a legal eagle one day.

But.... I would also fail the student for that assignment, for the student followed the letter of the assignment, but not the intent. The intent of such an assignment would surely be in the writing of your own sorting algorithm -- and not finding a class with a sorting algorithm included.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you asked them to provide their own sorting algorithm implementation without using Collections.sort or Comparators then they'd fail.
If you asked them to sort items without using Collections.sort or Comparators then (IMO) they'd pass but get marked down for providing a natural ordering that doesn't make sense
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

David O'Meara wrote:If you asked them to sort items without using Collections.sort or Comparators then (IMO) they'd pass but get marked down for providing a natural ordering that doesn't make sense

And yet, it's better advice not to be a wise guy. What if the professor doesn't feel the way you do.

I'm with Henry by the way - clearly the intent is to write your own sorting method. I might give the student an extension to do the assignment properly. But I find it hard to believe a student in an algorithms class doesn't know the point is to write an algorithm!


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
wood burning stoves
 
subject: How to Sort an Array List without using Collections.sort() method
 
Similar Threads
use comparator in jdk1.4
Generics, arrays, and sorting
reference variable
NullPointerException: Null attribute name while using <logic:Iterator> in Struts
How to sort Arraylist without Sort method