aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Natural ordering of strings and numbers 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 "Natural ordering of strings and numbers" Watch "Natural ordering of strings and numbers" New topic
Author

Natural ordering of strings and numbers

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

What are the rules for ordering strings? As an example, the output of the following code example suggests that the ordering rules used is different for strings and numbers.



Output:



What is the best way to know how a string will be sorted?
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

In case of String, "33" is not treated as per its numeric value. Since the first character '3' comes before 4, you see "33" before 4 and 5. However, TreeSet<Integer> compares the actual integer values.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Sorting Strings explains that for ascii characters the sorting of words would be based upon their ASCII/Unicode character values and it's true for the Unicode set as well (which includes the ASCII set).

The article speaks about the case when the Unicode order is not what we have in mind as the proper sorting mechanism.

Regards,
Dan


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

Joined: Oct 02, 2005
Posts: 430

Dan Drillich wrote:Sorting Strings explains that for ascii characters the sorting of words would be based upon their ASCII/Unicode character values and it's true for the Unicode set as well (which includes the ASCII set).

The article speaks about the case when the Unicode order is not what we have in mind as the proper sorting mechanism.

Regards,
Dan


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