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

Ordered and Sorted

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain howz order is defined? How to find which collection is ordered or unordered/sorted or unsorted?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic