File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Natural ordering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Natural ordering" Watch "Natural ordering" New topic
Author

Natural ordering

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

How is the natural ordering?
I mean If we have strings with numbers, letters, uppercase letters, spaces and other symbols, how can we know the order?

Natural ordering is
1. space
2. upercase
3. lowercase

but for
?. numbers
?. symbols

Thanks in advance
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
A class is said to have a natural ordering if it implements the Comparable interface, in which case it's ordering is decided by the implementation of the compareTo method. If you look at the javadoc for the String.compareTo method it explains how the order is decided.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Natural ordering is
1. symbols (#,@,(,&..)
2. numbers
3. space, ?, ¿
4. upercase
5. lowercase

symbols and space, ? are a mess :s
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Stuart A. Burkett wrote:A class is said to have a natural ordering if it implements the Comparable interface, in which case it's ordering is decided by the implementation of the compareTo method. If you look at the javadoc for the String.compareTo method it explains how the order is decided.


not just Comparable interface, but if a class implements Comparator interface then also it is said to have natural ordering. basically natural ordering means that you can COMPARE objects of the class using some property of the objects.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

gurpeet singh wrote:
Stuart A. Burkett wrote:A class is said to have a natural ordering if it implements the Comparable interface, in which case it's ordering is decided by the implementation of the compareTo method. If you look at the javadoc for the String.compareTo method it explains how the order is decided.


not just Comparable interface, but if a class implements Comparator interface then also it is said to have natural ordering. basically natural ordering means that you can COMPARE objects of the class using some property of the objects.



Actually Stuart is correct -- when the documentation talks about "natural ordering", it is referring to Comparable objects. You can achieve different sort orders with Comparators, but that is not considered in the documenation as "natural ordering".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

gurpeet singh wrote:not just Comparable interface, but if a class implements Comparator interface then also it is said to have natural ordering. basically natural ordering means that you can COMPARE objects of the class using some property of the objects.


That's not correct. A class can only have one natural ordering, and this should be one that "seems natural" - the most obvious ordering. For instance, a natural ordering of a Person class would probably be an ordering based on the last name, then first name. And we implement that using the Comparable interface.

A Comparator is used to provide ordering in something other than the natural order, and you can have as many of these orderings for a class as you want.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Joey Sanchez wrote:Natural ordering is
1. symbols (#,@,(,&..)
2. numbers
3. space, ?, ¿
4. upercase
5. lowercase

symbols and space, ? are a mess :s


Maybe... but unicode is based on ASCII for the range you are referring to, and ASCII is a standard that has been around since the 1960's. It may have quirks, but it is a standard that is accepted world wide -- and you can argue that consistency with a standard is less confusing.

Henry
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Henry Wong wrote:but unicode is based on ASCII for the range you are referring to


Absolutely and Natural sort order string comparison in Java - is one built in? says -

By "natural" sort order, I mean it compares strings the way a human would compare them, as opposed to "ascii-betical" sort ordering that only makes sense to programmers. In other words, "image9.jpg" is less than "image10.jpg", and "album1set2page9photo1.jpg" is less than "album1set2page10photo5.jpg", and "1.2.9.1" is less than "1.2.10.5"


Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Henry Wong wrote:
gurpeet singh wrote:
Stuart A. Burkett wrote:A class is said to have a natural ordering if it implements the Comparable interface, in which case it's ordering is decided by the implementation of the compareTo method. If you look at the javadoc for the String.compareTo method it explains how the order is decided.


not just Comparable interface, but if a class implements Comparator interface then also it is said to have natural ordering. basically natural ordering means that you can COMPARE objects of the class using some property of the objects.



Actually Stuart is correct -- when the documentation talks about "natural ordering", it is referring to Comparable objects. You can achieve different sort orders with Comparators, but that is not considered in the documenation as "natural ordering".

Henry


Thanks, that make me this much more clear.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Dan Drillich wrote:
Absolutely and Natural sort order string comparison in Java - is one built in? says -

By "natural" sort order, I mean it compares strings the way a human would compare them, as opposed to "ascii-betical" sort ordering that only makes sense to programmers. In other words, "image9.jpg" is less than "image10.jpg", and "album1set2page9photo1.jpg" is less than "album1set2page10photo5.jpg", and "1.2.9.1" is less than "1.2.10.5"



Dan,

Not sure of the point you are getting at. This links to some definition of "natural ordering" that a particular user want. And other users point out, that it doesn't work that way -- and it is only one example, which has tons of edge conditions which this user never bother explaining. In other words, it is basically a complaint, with a single example of what it should do (and lots of hand waving for other cases).

Henry

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Natural ordering