Hi

YANAGAWA Kazuhisa wrote:

> schneik / us.ibm.com writes:
>
> > which isn't what I normally want, I would like to do this:
> >
> >     # ruby -e 'print ["computer ","Boston ", "Austin ", "another
> > "].dict_sort, "\n"'
> >    another Austin Boston computer
>
> Well, below is not sufficient?
>
> # filter is deprecated on 1.5.x.  use collect! instead.
>
>     > ruby -e 'puts  ["computer ","Boston ", "Austin ", "another "].\
>     filter {|e| [e.downcase, e]}.sort.filter {|k, e| e}'
>     another
>     Austin
>     Boston
>     computer

Sure, that is sufficient, but Perl is sufficient too. :-) Also people tend to
use different variable names, and some people might do e.upcase, so the
intentions of the differing results are not so straightforward. You always have
to stop and think about what the code is really doing. I think that having to
stop and think is OK (as long as you don't have to think as much as you have to
for Perl for the same amount of work)--except in the case of things that
unnecessarily obscure simple tasks that are very common.

Despite the unfortunate choices made in the ancient ASCII character mapping
standard,  the _most-common_ and _most-natural_ way to think of sorting is in
dictionary order, and I think it would be better to express this directly,
rather than every person having to in effect reinvent such a commonly used wheel
each time they use it (or to define their own version of it). Ironically sort
doesn't do (what for most people is) the natural type of sort. This is why I
would like a sort_dict_order that is just as simple as sort and that is used in
exactly the same way as sort.


> This `Schwartzian Transform' method is famous in Perl community.  A
> good name is presented, this may be introduced to ruby standard
> library :-)
>
>     class Array
>       def sort_with_key
>         # to preserve original array, first filter is replaced with collect.
>         collect { |e| [yield(e), e] }.sort.filter { |k, e| e }
>       end
>     end

Considering how widely used sorting operations are used, I think a standard
library schwartz_sort is probably a good idea. I'll vote for that.
--
Conrad Schneiker
(This note is unofficial and subject to improvement without notice.)