Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > what is natural order ?

Reply
Thread Tools

what is natural order ?

 
 
gk
Guest
Posts: n/a
 
      11-03-2006
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 ?

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      11-03-2006
gk wrote:
> 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 ?


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/...omparable.html

-cheers,
Manish

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-03-2006
Manish Pandit wrote:
> gk wrote:
>
>>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 ?

>
>
> 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. [...]


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.

--
Eric Sosman
lid
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-03-2006

"gk" <> wrote in message
news: ups.com...
> 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


 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      11-03-2006
gk <> wrote:
>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 <http://www.dagon.net/>
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      11-03-2006
Eric Sosman wrote:

> 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


 
Reply With Quote
 
Tom Forsmo
Guest
Posts: n/a
 
      11-03-2006
Eric Sosman wrote:
> 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
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-03-2006

"Tom Forsmo" <> wrote in message
news:...
>
> 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
>
> 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.


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


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-03-2006


Tom Forsmo wrote On 11/03/06 15:59,:
> Eric Sosman wrote:
>
>> 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.


You might be interested in

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

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

--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I trust on key %hash natural order? filippo Perl Misc 13 06-16-2006 06:01 PM
Natural sort order Paul Java 1 09-14-2004 02:44 PM
Natural sorting order for alphanumeric fields Paul Java 1 09-14-2004 12:41 PM
conversion: natural -> time Simone Winkler VHDL 1 04-01-2004 10:14 PM
Natural order sort Alan Davies Ruby 2 09-15-2003 04:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57