File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Deepika Saxena
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic