| Author |
Collection framework doubt , what is the difference between Ordering and Sorting
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
what is the difference between Ordering and Sorting ... I mean both seem to be the same to me
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3859
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
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
Joined: Mar 17, 2011
Posts: 4898
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
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
Joined: Oct 13, 2005
Posts: 32827
|
|
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);
|
 |
 |
|
|
subject: Collection framework doubt , what is the difference between Ordering and Sorting
|
|
|