Win a copy of Penetration Testing Basics this week in the Security forum!

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

Vishal Hegde
Ranch Hand
Posts: 1077
what is the difference between Ordering and Sorting ... I mean both seem to be the same to me

Matthew Brown
Bartender
Posts: 4568
9
• 1
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
Posts: 21694
85
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
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
Posts: 10527
64
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
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
Posts: 50617
82
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.