Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

what  is natural order ?

Thread view: 
gk - 03 Nov 2006 06:00 GMT
TreeSet maintains its elements in their natural order, hence iterating
will produce "Apple Banana Cricket " instead of "Apple Cricket Banana"

what is natural order in the above ?

does it want to say, treeset always  sorts automatically by
ALPHABATICALLY ?
Manish Pandit - 03 Nov 2006 06:13 GMT
> TreeSet maintains its elements in their natural order, hence iterating
> will produce "Apple Banana Cricket " instead of "Apple Cricket Banana"
[quoted text clipped - 3 lines]
> does it want to say, treeset always  sorts automatically by
> ALPHABATICALLY ?

Yes, in this case - as it contains strings. String implements
comparable, which offers natural ordering. In case of strings, the
natural ordering implies alphabetical sorting. You might want to take a
look at Comparable Interface for more information on this.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

-cheers,
Manish
Eric Sosman - 03 Nov 2006 14:31 GMT
>>TreeSet maintains its elements in their natural order, hence iterating
>>will produce "Apple Banana Cricket " instead of "Apple Cricket Banana"
[quoted text clipped - 7 lines]
> comparable, which offers natural ordering. In case of strings, the
> natural ordering implies alphabetical sorting. [...]

    Almost.  The natural ordering for String objects is their
lexicographic order according to the Unicode values of their
individual characters, so (for example) "Aïda" comes after
"Axolotl".  For an even more blatant violation of alphabetical
order, note that "Zebra" precedes "aardvark".

    Also, "alphabetical order" varies from place to place, even
if you consider only languages written in Latin alphabets.  As
far as I know (I'm no expert on this, just someone who once got
a bit of a scolding from a person who was), everybody agrees on
the ordering of the twenty-six unaccented letters, but the
treatment of accented letters is subject to national and linguistic
variation.

    See also java.text.Collator and allied classes.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Chris Uppal - 03 Nov 2006 19:04 GMT
>      Almost.  The natural ordering for String objects is their
> lexicographic order according to the Unicode values of their
> individual characters, so (for example) "Aïda" comes after
> "Axolotl".  For an even more blatant violation of alphabetical
> order, note that "Zebra" precedes "aardvark".

All true, and undoubtedly all that the OP needs to know.

But it may be (midly) interesting to note that -- although String /claim/ to
sort lexicographically according to the Unicode code points -- they /actually/
sort by the UTF-16 values.  And that doesn't produce the same sort order for
Unicode characters outside the 16-bit range.

>      Also, "alphabetical order" varies from place to place, even
> if you consider only languages written in Latin alphabets.  As
> far as I know (I'm no expert on this, just someone who once got
> a bit of a scolding from a person who was), everybody agrees on
> the ordering of the twenty-six unaccented letters,

For interest: there are exceptions.  Or at least, there are according to the
Unicode people.  Spanish (they say) traditionally considers ll to be a digraph
falling between l and m.

   -- chris
Tom Forsmo - 03 Nov 2006 21:59 GMT
>     Almost.  The natural ordering for String objects is their
> lexicographic order according to the Unicode values of their
> individual characters, so (for example) "Aïda" comes after
> "Axolotl".  For an even more blatant violation of alphabetical
> order, note that "Zebra" precedes "aardvark".

I have allways though of natural ordering as the way we humans would
order strings, e.g.

string1
string2
string...
string9
string10
string11
..
string20
string21

and other similar sort order, instead of the typical

string1
string10
string11
...
string2
string20
string21

etc and other similar types of sorting problems

Is there a name for the "human sort order"? and does there exists an
implementation of it. Perhaps a name could be "semantic sort order".
I realise that it would require quite a big implementation with many
special case rules that in no way can be generalised and made into a
single algorithm like lexical or natural orders.

tom
Oliver Wong - 03 Nov 2006 22:11 GMT
> I have allways though of natural ordering as the way we humans would order
> strings, e.g.
[quoted text clipped - 14 lines]
> special case rules that in no way can be generalised and made into a
> single algorithm like lexical or natural orders.

   There may be a name for the particular sorting you've shown above
(though the sort order is ambiguous, because there are some corner cases you
haven't demonstrated in that example), but I doubt that there exists a
"human sort order", as different humans are likely to sort things in
different orders.

   See Chris' post about how a Spanish speaking person would likely sort
strings in different order than an English speaking person, for example.

   - Oliver
Eric Sosman - 03 Nov 2006 23:49 GMT
Tom Forsmo wrote On 11/03/06 15:59,:

>>    Almost.  The natural ordering for String objects is their
>>lexicographic order according to the Unicode values of their
[quoted text clipped - 32 lines]
> special case rules that in no way can be generalised and made into a
> single algorithm like lexical or natural orders.

   You might be interested in

    http://sourcefrog.net/projects/natsort/

(Yes, that's "frog" as in "Kermit.")

Signature

Eric.Sosman@sun.com

Oliver Wong - 03 Nov 2006 17:20 GMT
> TreeSet maintains its elements in their natural order, hence iterating
> will produce "Apple Banana Cricket " instead of "Apple Cricket Banana"
>
> what is natural order in the above ?

   It might be clearer to talk about what is NOT natural order.

   In some cases, you can provide a sorting algorithm with a comparator.
The comparator defines an ordering. For example, you might create a
Comparator<Integer> which orders all odd integers before all even integers,
so that the the order of {1,2,3,4,5} would be: (1,3,5,2,4). In other words,
by using a Comparator, you can invent any kind of ordering you want.

   When you DON'T use a comparator, it implies you want the natural
ordering of the elements, instead of your custom ordering.

   - Oliver
Mark Rafn - 03 Nov 2006 18:46 GMT
>TreeSet maintains its elements in their natural order, hence iterating
>will produce "Apple Banana Cricket " instead of "Apple Cricket Banana"

Natural order is the ordering provided by the compareTo() methods
of the elements in the collection (all of which must implement Comparable).

For String, this is asciibetical (more specifically, compared according to the
unicode value of each character).

>what is natural order in the above ?

"Apple Banana Cricket".

>does it want to say, treeset always  sorts automatically by
>ALPHABATICALLY ?

No.  TreeSet can store Comparable objects other than String.  Each class
defines it's own natural ordering in the compareTo() method.  And further, no.
String's natural ordering is not alphabetical, it's based on character values,
meaning that if you have mixed upper and lowercase, you'd get "Banana apple
cricket".

If you want some other sort order, you need to instantiate the TreeMap with a
Comparator that does what you want.  The String.CASE_INSENSITIVE_ORDER
Comparator is handy :)
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.