• Post Reply Bookmark Topic Watch Topic
  • New Topic

Natural Order  RSS feed

 
SrinivasaRao Madugula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can any one explain me about "Natural Order" in Collections. I am not able to get the concept of it.

Thanks in advance.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by SrinivasaRao Madugula:
Hi, can any one explain me about "Natural Order" in Collections. I am not able to get the concept of it.

Thanks in advance.


I think this link will answer your question.
 
SrinivasaRao Madugula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You provided me the exact answer. Thank you very much.
 
sreedhar Lackka
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I tried by clicking on the Link which is given Chandrashekar but could not find any answer rather I got books links.

Could you please give some inputs on the same.

Regards,
Sree
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SrinivasaRao Madugula wrote:Hi, can any one explain me about "Natural Order" in Collections. I am not able to get the concept of it.

Thanks in advance.


It depends on you collections, for example, say, you have characters object, then natural order is their unicode values, for numbers natural order is as usual, ascending order, for String check it with java API.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for natural sorting there is one one interface "java.lang.Comparable". for example "TreeSet, TreeMap" internally uses this interface. In this interface "public int compareTo(Object o)" method is avilable. pre defined classes are if already comparable it means that classes implements "Comparable" interface then only natural sorting order is possible in that classes(other possibility there but default sorting not available if not comparable). That classes internally calls "compareTo()". When natural sorting is required heterogeneous objects are not allowed and null insertion is not possible.
implementation of compareTo():

returns -ve if object1 has come before object2(object1 <object2)
returns +ve if object1 has come after object2(object1 >object2)
returns 0 if object1 and object2 are equal(object1 =object2)

example
for predefined non comparable java classes natural sorting order is not available (by using Comparable interface)

remaining things digg from othe resources
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!