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

Sorting in Java

 
Vivek Jain
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 Questions:
1. If I want to sort objects not more than 50, how do I do it, i mean collections.sort or something else???

2. If I want to sort more than 1000 objects, how do i do it???

Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek,
Collections.sort() will work with any size collection.
 
Vivek Jain
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it'll work but which is the best efficient method???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.sort() is just one method. It's the best choice in general for the vast majority of situations you might find yourself in.

If you really need to optimize beyond that, you will need to be much more speicfic about the specific situation you're dealing with. There is no single method that is always the best - it depends on the situation. There are many different sorting algorithms which are possible, and many of them are optimized for particular situations. The modified heapsort used by Collections.sort() is a good all-around algorithm, but it's not always the best. Feel free to implement an alternate algorithm if it's better for your particular dataset.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer to almost every software question of the nature "What is the best way to do XYX" is usually "It depends".

You can take a semester long college class on algorithms (I did). You learn how to analyze your data and which algorithm is the best for that situation.

This is not true just for sorting, although sorting and searching are a BIG chunk of it.

So, without knowing anything about your data, there is no 'best' answer. A bubble sort might be the best, or a quick sort.

Heck, even for several thousand objects, it may not make much difference. But it might.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic