• 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

Natural ordering

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Natural ordering is
1. symbols (#,@,(,&..)
2. numbers
3. space, ?, ¿
4. upercase
5. lowercase

symbols and space, ? are a mess :s
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Joey Sanchez
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic