Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between Ordering and Sorting ... I mean both seem to be the same to me
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Marshal
Posts: 75636
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic