File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ordered and Sorted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ordered and Sorted" Watch "Ordered and Sorted" New topic

Ordered and Sorted

gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Can somebody explain howz order is defined? How to find which collection is ordered or unordered/sorted or unsorted?
Nikhil Goel
Ranch Hand

Joined: Jul 11, 2003
Posts: 32
Ordering & Sorting are 2 distinct names to same concept . Java has the concept of Natural Ordering means objects like Strings are self ordered in java .
Hence collections that contain string as key or string as objects are by default sorted .

Collections like List,Set ,Map need sorting . There are variants to these also like SortedSet , TreeSet etc .
ArrayList,HashMap,Hashtables need sorting...

If you want your objects to be sorted you have to implement a Comparator interface for that . It has a method compareTo(Object 1 , Object2) that sorts 2 objects while adding them in Collections.

There is also a utility method sort(Collection collection) on Collections class that sorts the collection .
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
Note that something that is ordered may not be sorted. Ordering means that the order of access is defined, e.g. I get stuff back in the order that I put it in. It may not however be sorted in any way.

SCWCD: Online Course, 50,000+ words and 200+ questions
I agree. Here's the link:
subject: Ordered and Sorted
It's not a secret anymore!