File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort Arraylist without Sort method

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to sort Arraylist without Collections.sort(alist). Is there any option sort arraylist objects
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By implementing a sort algorithm (e.g bubble sort) yourself.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to use get(int) and set(int, E) extensively to swap elements.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be better to dump the contents of the List into an array, sort the array, and create a new List from the sorted array.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that if Rajendra doesn't want to use Collections.sort (or isn't allowed to, perhaps this is a homework assignment) that Arrays.sort is also out of the question.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't mean to use Arrays#sort, but to implement a sorting algorithm on the resultant array.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah right. I thought that with "sort the array" you meant using Arrays.sort.

That said, I don't see what advantage the arrays give you when sorting an ArrayList. The latter's get and set methods are just as fast as direct array access. (Granted, for a LinkedList it would probably be wiser to first extract the data.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Direct array access might be easier for a newbie to visualise than get(123) and set(123, foo). At least that is what I was thinking. . . .
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic