Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection framework doubt , what is the difference between Ordering and Sorting

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between Ordering and Sorting ... I mean both seem to be the same to me
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ordered collection maintains a specific order. It doesn't have to be sorted, there's just some notion of an order - often based on the order things are added to it. So an ArrayList is ordered. A LinkedHashSet is ordered. A HashSet isn't - there is no concept of an order, and if you iterate through it you aren't guaranteed to get the same order every time.

A sorted collection is a special case of an ordered collection where the order is determined by the values. An ArrayList isn't sorted (unless you sort it yourself), but a TreeSet is.

Does that help?
 
Henry Wong
author
Marshal
Pie
Posts: 21126
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:what is the difference between Ordering and Sorting ... I mean both seem to be the same to me



Or as a side statement, you can say that all sorted collections are ordered, but not all ordered collections are sorted.

Henry
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Vishal Hegde wrote:what is the difference between Ordering and Sorting ... I mean both seem to be the same to me



Or as a side statement, you can say that all sorted collections are ordered, but not all ordered collections are sorted.

Henry



Dear Henry,

Please provide me an example for the same so that it will be easy for me to differntiate the difference between ordered and Sort
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:lease provide me an example for the same so that it will be easy for me to differntiate the difference between ordered and Sort

I believe Matthew already did. Sorted collections (like TreeSet) maintain their elements in a predetermined sorted sequence, defined either by a Comparator or - when elements are Comparable - by their natural order. Ordered collections have a predictable order, but it's usually (as Matthew said) based on the order they were added.

What else don't you understand?

Winston
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok noww

1 ,2,3,4,5,6,7,8,9, they are in Order right and sorted in ascending Order or

A,B,C,D,E,G,H etc is also ordered right and also sorted in ascending order

now how can i differntiate sort and ordering in this scenario


 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote: . . .

1 ,2,3,4,5,6,7,8,9, they are in Order right and sorted in ascending Order or

A,B,C,D,E,G,H etc is also ordered right and also sorted in ascending order . . .
You can’t tell whether those are ordered.
If you created the first series like this: add(1) add(9) add(2) add(8) add(3) add(7) add(4) add(6) add(5), then they are out of order.

“Sorted” is a word with a precise meaning, but “ordered” isn’t. By saying ordered you usually mean in the same order that elements were inserted. But you can insert elements into a List in locations earlier than elements already in the list, eg using add(1, E);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic