Kenneth,
Below the cut is my example implementation as I understand your
requirements.
Note that the "compare" routine uses $a and $b which are "special" to perl
sort routines.
Also the compare routine is written for obviousness rather than for brevity
or elegance.
The return from compare illustrates Shalomi Fish's point about using the
"||" operator to compose sort fields.
Descending numeric order is done by reversing the comparison on that sub
field.
chris
----- cut -----
#!/usr/env/bin perl
use strict;
use warnings;
my @x = <DATA>;
sub compare {
my @a = split(/\t/, $a);
my @b = split(/\t/, $b);
return $b[0] <=> $a[0] || $a[1] cmp $b[1]
}
print for (sort {compare} @x);
__DATA__
9500 ohzaew
5300 dohpha
0700 liemah
1700 phuhei
0200 phuowo
1300 ojaeng
3900 aebaat
4200 dohgha
4200 aiyiej
6300 ojaeng
1600 haequa
3100 hupiez
3200 ahrieb
3600 ohzaew
5300 queebe
2000 oeyael
0200 hahwoo
9900 shahye
9300 johhir
6400 shahye
4500 ohfici
5500 ahngoh
7300 aibove
8200 ahrieb
9100 ohzaew
3100 ohzaew
2800 gahnoh
0800 aedeng
8400 oowaih
0300 vouroh
1400 shahye
0500 ciejee
0500 uanahp
2100 ophuum
1500 aideev
6900 aegeuw
6300 haequa
9300 queebe
5400 reogai
5000 ophuum
1700 aebaat
1600 eshida
3700 beidae
5200 quieki
6800 eashoo
6800 ohweba
2300 apahqu
8100 ahghee
6700 jooxoj
3500 yeiboo
2800 chuema
On Fri, Jun 17, 2016 at 3:41 PM, Kenneth Wolcott <[email protected]>
wrote:
> On Fri, Jun 17, 2016 at 2:33 PM, Kenneth Wolcott
> <[email protected]> wrote:
> > Hi;
> >
> > I'm having trouble understanding the built-in Perl sort with regards
> > to mixed numbers and strings
> >
> > I'm looking at http://perldoc.perl.org/functions/sort.html
> >
> > I have an array that I want to have sorted numerically and descending.
> >
> > The array is composed of elements that look like the following regex:
> >
> > ^\d+\t\[a-zA-Z0-9]+$
> >
> > I always have "use strict" at the top of my Perl scripts.
> >
> > If I try:
> >
> > my @articles = sort {$b <=> $a} @files;
> >
> > I get error(s)/warning(s) that the data is not numeric.
> >
> > if I try:
> >
> > my @articles = sort {$b cmp $a} @files;
> >
> > I will get numbers sorted as letters, not numerically.
> >
> > I tried to understand the sort perldoc page further down, but did
> > not grok it at all.
> >
> > What I did as a workaround was to implement my own extremely
> > brute-force sort routine, which works, but is very ugly.
> >
> > Since I have very few elements (perhaps as many as a couple dozen),
> > the inefficiency is immaterial.
> >
> > I'd rather that my code be correct, intuitive and elegant (and
> efficient).
> >
> > Thanks,
> > Ken Wolcott
>
> Addendum:
>
> It appears that when the sequence of digits is the same length in all
> instances that the data will be sorted correctly, but when the length
> of the sequence of the digits is not the same in the entire data set,
> that is when the sort results will be incorrect.
>
> My most current data with this reverse character sort mechanism works
> correctly, but I'd like it to work in all cases.
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>