• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic