• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

What's different between ordered and sorted?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I found many mock exam questions about collection asked if the collection is ordered or in natural ordered or sorted. What's the difference?
Sorry English is my third language, I have some difficulties on understanding such details.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorted means there is a Comparator method based on which the elements are added into the collection. Ordered means the collection is maintained in the list in which the programmer adds the elements.
HTH
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When u add a few elements to a collection, and say, if the collection maintains the order in which u inserted (that is, while printing it back, u get the same order), it is 'ordered'. Ordered means that the elements are NOT stored in a random manner (that doesnt make sense to us).
Sorting is also a form of ordering. Sorting means, ordering based on a 'natural order', that is, for numbers, the natural order is 1,2,3 etc. and for letters it is A,B,C... You can also specify what u exactly mean by a natural order, for your own classes.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sun par:
Sorted means there is a Comparator method...


Well, actually, Comparator is an interface, but Sunita is on the right track. Comparator defines the methods compare and equals. So, based on those methods, you can compare multiple objects of some type and determine how they should be sorted. For example, the class Integer, implements the Interface Comparable (which is very similar to Comparator) so you can compare two Integer objects to determine which one should come first in a list. If you've sorted objects this way, they are considered to be sorted.
Note that, if a class does not have a compareTo method or something similar, there may be no way to sort a collection of instances of that class. There is really no way to know which one should come before another.
Ordered, on the other hand, means that the objects are kept in the order that they were originally put there. So, for example, if you were adding things to a list, that list might keep the objects sorted (meaning that it will insert the new element wherever it belongs to keep the list sorted, as discussed above) or it might just put the object on the end, meaning that it retains the order that you put the elements in. Or, it is possible to make a container that puts elements in by some other criteria, making it neither sorted nor ordered.
I hope that helps,
Corey
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic