Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# java.util

geeta rai
Ranch Hand
Posts: 48
What is the difference between ordered and sorted?

venkatesh rajmendram
Ranch Hand
Posts: 130
In collections can have either ordered or sorted or both...if it is sorted its implicitly ordered ( sort is kind of order) well here is the e.g
When you enter something in a collection say
tom
tim
apple
orange
and when you access it will come in the same order as above.
in case as sorted when you access the collection you will get
apple
orange
tim
tom
I guess that helps
- Venkatesh

srini v
Greenhorn
Posts: 11
Hi:
Order: as per definition, mean to put things into their proper places in RELATION to each other.
eg., Arrange the given numbers in ascending ORDER
5 2 4 6
Result:
2 4 5 6

Sort: as per definition, a GROUP set up on the basis of any characteristic in common
eg., Sort the following magazines by their catagory.
HBR, PC World, ACM Magazine, BusinessWeek, Fortune
Result:
Computer Magazines: PC World, ACM
Hope this helps.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
I have to disagree with srini here; that doesn't make any sense in this context. Venkatesh's explanation is much more accurate. If a collection is ordered, that means that the sequence that the elements occur in is significant. If you're using a List, there's a difference between 1, 2, 3 and 1, 3, 2. If you'r using a Set, there's no difference (or if there is, you should ignore it as unimportant). That's because a List is ordered, and a Set isn't.
Now to sort a List is to put it in a particular order, according to some rule. In the collections framework the rule is always provided by a compareTo() or compare() method, as defined in the Comparable or Comparator interfaces. All sorted collections are also ordered, because if the order wasn't signficant, there's be no point to trying to sort it. But not all ordered collections are sorted.

srini v
Greenhorn
Posts: 11
Dear Jim: