sort of about sort 
Author Message
 sort of about sort

Just for interest why wasn't sort implemented so you could use it like
the UNIX sort.

numeric sort.

It would seem to make sense because most of the time you either want to
sort in alpha order or numeric.
Without wanting to change the language is this something that could be
perl 6 ?

John.



Tue, 29 Jul 2003 09:11:13 GMT  
 sort of about sort

Quote:

>Just for interest why wasn't sort implemented so you could use it like
>the UNIX sort.

Because that would be limiting. It can do what sort(1) does
(sans the disk file/merge sort thing), plus some more.

Perl's sort() _does_ have that feature, but we spell "-n" as "{$a <=> $b}".

Quote:
>even sort -nr to reverse a
>numeric sort.

That we spell "{$b <=> $a}".

Quote:
>It would seem to make sense because most of the time you either want to

                                     ^^^^^^^^^^^^^^^^

Quote:
>sort in alpha order or numeric.

But other times you don't.

Quote:
>Without wanting to change the language is this something that could be
>perl 6 ?

I hope not. I want to be able to sort my way all of the time,
not merely most of the time.

Besides we can already do what needs doing, no new feature needed.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 29 Jul 2003 11:54:24 GMT  
 sort of about sort

Quote:

>Just for interest why wasn't sort implemented so you could use it like
>the UNIX sort.

>numeric sort.

The current method is more flexible and useful.

Quote:
>It would seem to make sense because most of the time you either want to
>sort in alpha order or numeric.

No.  Perhaps most of the time *you* want an alpha or numeric sort;
that doesn't mean that that is what other people want.

Quote:
>Without wanting to change the language is this something that could be
>perl 6 ?

It could, but it won't, because it multiplies entities unnecessarily.
There is already a perfectly good way to do a numeric reversed sort
(as you know, having already read the manual entry for 'sort') and
your suggestion only adds another way to do exactly the same thing.

If you really want that, it is easy to add:


          my $arg = shift;    
          my $numeric  = ($arg =~ /n/);
          my $reversed = ($arg =~ /r/);




        }

Then you can use:


and nobody else needs to be bothered by it.

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Tue, 29 Jul 2003 13:24:44 GMT  
 sort of about sort

Quote:



> >Just for interest why wasn't sort implemented so you could use it like
> >the UNIX sort.

> >numeric sort.

> The current method is more flexible and useful.

[SNIP]

Quote:

> It could, but it won't, because it multiplies entities unnecessarily.
> There is already a perfectly good way to do a numeric reversed sort
> (as you know, having already read the manual entry for 'sort') and
> your suggestion only adds another way to do exactly the same thing.

So what does TMTOWTDI stand for again. ;)
I was just thinking that it could (have) be(en) implemented so if you want to
sort by another way you could define your own or by default it does alpha or with
a switch it does numeric.

I was only asking because perl started on UNIX and I'm sure Larry often used sort
-nr.  I have no problem with doing it the way it is now I was just curious as to
why it was set up the way it was.

John.



Tue, 29 Jul 2003 17:02:03 GMT  
 sort of about sort


Quote:

>So what does TMTOWTDI stand for again. ;)

You're confused.  Just because there's more than one way to do it
doesn't mean that the Perl core must necessarily support every
possible way of doing everything.  

        Although the Perl Slogan is There's More Than One Way to Do
        It, I hesitate to make 10 ways to do something. :-)


Quote:
>I was just thinking that it could (have) be(en) implemented so if you
>want to sort by another way you could define your own or by default
>it does alpha or with a switch it does numeric.

Right.  And I was just pointing out that if you want that, you can
already get it (so there *is* more than one way to do it, including
yours) and then you can pay the cost for it if you like, and nobody
else has to.

Quote:
>I was only asking because perl started on UNIX and I'm sure Larry
>often used sort -nr.

Yes, and being familiar with unix he must have realized that the
qsort(3) functionality was much more useful and appropriate than the
sort(1) functionality.
rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


Tue, 29 Jul 2003 19:40:57 GMT  
 sort of about sort

Quote:


>> It could, but it won't, because it multiplies entities unnecessarily.
>> There is already a perfectly good way to do a numeric reversed sort
>> (as you know, having already read the manual entry for 'sort') and
>> your suggestion only adds another way to do exactly the same thing.

> So what does TMTOWTDI stand for again. ;)
> I was just thinking that it could (have) be(en) implemented so if you want to
> sort by another way you could define your own or by default it does alpha or with
> a switch it does numeric.
> I was only asking because perl started on UNIX and I'm sure Larry often used sort
> -nr.  I have no problem with doing it the way it is now I was just curious as to
> why it was set up the way it was.

In this case, there really is, IMHO, justification for OMWTDI: default
sorts are significantly faster (by a constant factor of course; the time
complexity doesn't change) than sorts using a comparison block (which is
the whole basis for the Guttman/Rosler transformation).  Thus there's a
time penalty for doing a numeric sort compared to a lexical sort, and
there's a time penalty for a descending sort compared to an ascending
sort.


Tue, 29 Jul 2003 19:56:38 GMT  
 sort of about sort

Quote:

>In this case, there really is, IMHO, justification for OMWTDI:
>default sorts are significantly faster than sorts using a comparison
>block

Yes, but the Right Thing To Do is to recognize those sorts at compile
time and optimize them.  Greg Bacon contributed a patch some time ago
to do precisely that; the optimization is in 5.6.0.  If you write

        sort { $a cmp $b } ...

then it compiles as if you had written

        sort ...;

and similarly, if you ask for

        sort { $b <=> $a } ...

then it uses a special-purpose built-in comparison function analogous

The relevant code is the function S_simplify_sort in the vicinity of
line 6052 of op.c in the 5.6.0 distribution.

rd
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print



Thu, 31 Jul 2003 20:42:48 GMT  
 sort of about sort




:
: >In this case, there really is, IMHO, justification for OMWTDI:
: >default sorts are significantly faster than sorts using a comparison
: >block
:
: Yes, but the Right Thing To Do is to recognize those sorts at compile
: time and optimize them.  Greg Bacon contributed a patch some time ago
: to do precisely that; the optimization is in 5.6.0. [...]

Here I should mention that Hans Mulder did some considerable cleanup
and reworking to ready the patch for (I think) 5.6.0.

I wonder how much of a win it would be to have a special builtin
comparison for Schwartzian Transforms, e.g., $a->[0] <=> $b->[0].  Would
the invariant have to go in the 0th position?  Joseph Hall pointed out
that it would be nice if there were an XS interface for providing one's
own quick comparisons.

Greg



Sat, 02 Aug 2003 01:03:20 GMT  
 sort of about sort

Greg> I wonder how much of a win it would be to have a special builtin
Greg> comparison for Schwartzian Transforms, e.g., $a->[0] <=> $b->[0].  Would
Greg> the invariant have to go in the 0th position?

I try to always teach it that way, although I think the posting
which triggered the naming of the transform had the original data
in the [1] element, not [0].

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Sat, 02 Aug 2003 01:54:02 GMT  
 sort of about sort

  GB> I wonder how much of a win it would be to have a special builtin
  GB> comparison for Schwartzian Transforms, e.g., $a->[0] <=> $b->[0].
  GB> Would the invariant have to go in the 0th position?  Joseph Hall
  GB> pointed out that it would be nice if there were an XS interface
  GB> for providing one's own quick comparisons.

you have to handle many possible cases. string vs. number, normal or
reverse order, etc. also supporting multiple keys is an issue.
extracting the key into $a->[0] is another important issue. by using the
GRT you remove the need for a special XS callback as well. i have a
partially written moduledust gathering that supports complex key
extraction and autogenerates that code and then performs a GRT sort on
the data. it needs a fair amount of work but the principle design ideas
are valid. if anyone wants to take it on i will be glad to help. i don't
have the time to drive that project. it has fairly ambitious goals and
requires some decent coding skills to make it clean and fast. any
volunteers?

among other things, the module could be used to generate code that can
be cut and pasted into your programs. we could even support the common
cases directly so again no need for special XS code.

uri

--

SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com



Sat, 02 Aug 2003 03:12:50 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. re-sorting a sorted list (and sorting by date)

2. YAPSQ (Perl Style Question): sorting and maintaining sorted order

3. strange SEGV with sort or some sort of such thing

4. sort with dynamic sort expression help

5. insert to sorted list via sort?

6. Sort of a sort problem.

7. "Packed Default" sort - Where is Sort::Records module?

8. SORT: trying to sort files on date. AND connecting system commands via pipe ????

9. Sorting within a sort... (is it even possible?)

10. Perl sort different from unix sort

11. SORT sort hash with alpha-numeric keys

12. Sorting a hash by value and sub sorting the keys

 

 
Powered by phpBB® Forum Software