# [Q]: How to sort first abc, then 123?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi, All:

my @oldlist =
(And64,And1,And16,And8,And24,And32,Nand64,Nand1,Nand16,Nand8,Nand24,Nand32);

I want the sorted output like: (first grouped aphabetically, then sorted by
number)

And1
And8
And16
And24
And32
And64
Nand1
Nand8
Nand16
Nand24
Nand32
Nand64

I tried this:

my @sort1 = sort by_abc123     @oldlist;
sub by_abc123 {
\$at = \$a;
\$bt = \$b;
\$aa = \$at =~ s/([A-Za-z_]*)([\d]*)/\$1/;
\$ba = \$bt =~ s/([A-Za-z_]*)([\d]*)/\$1/;
\$a1 = \$a =~ s/([A-Za-z_]*)([\d]*)/\$2/;
\$b1 = \$b =~ s/([A-Za-z_]*)([\d]*)/\$2/;
( \$aa cmp \$ba || \$a1 <=> \$b1 );
}

But it certainly doesn't work... :)
Any help is appreciated.

HEB

## Re: [Q]: How to sort first abc, then 123?

> Hi, All:

Hi,

Just out of curiosity: Why do you have X-No-Archive set?

>
> my @oldlist =
> (And64,And1,And16,And8,And24,And32,Nand64,Nand1,Nand16,Nand8,Nand24,
> Nand32);
>
> I want the sorted output like: (first grouped aphabetically, then
> sorted by number)

....

> I tried this:

use strict;
use warnings;

> my @sort1 = sort by_abc123     @oldlist;
> sub by_abc123 {
>         \$at = \$a;
>         \$bt = \$b;
>         \$aa = \$at =~ s/([A-Za-z_]*)([\d]*)/\$1/;
>         \$ba = \$bt =~ s/([A-Za-z_]*)([\d]*)/\$1/;
>         \$a1 = \$a =~ s/([A-Za-z_]*)([\d]*)/\$2/;
>         \$b1 = \$b =~ s/([A-Za-z_]*)([\d]*)/\$2/;
>         ( \$aa cmp \$ba || \$a1 <=> \$b1 );
> }
>
> But it certainly doesn't work... :)

What do you mean, "doesn't work"? Please read the posting guidelines for
this group. They are posted here regularly.

> Any help is appreciated.

Your basic problem is that you did not check what the s/// operator
returns. Take a look in perldoc perlop.

#! /usr/bin/perl

use strict;
use warnings;

my @orig = qw(And64 And1 And16 And8 And24 And32
Nand64 Nand1 Nand16 Nand8 Nand24 Nand32
);
my @sorted = sort lexicographic_sort @orig;
print "@sorted\n";

sub lexicographic_sort {
my (\$a_alpha, \$a_digits) = (\$a =~ /^([[:alpha:]]*)([[:digit:]]*)\$/);
my (\$b_alpha, \$b_digits) = (\$b =~ /^([[:alpha:]]*)([[:digit:]]*)\$/);
(\$a_alpha cmp \$b_alpha) || (\$a_digits <=> \$b_digits);
}
__END__

D:\Home\asu1\UseNet\clpmisc> v
And1 And8 And16 And24 And32 And64 Nand1 Nand8 Nand16 Nand24 Nand32
Nand64

Sinan

## Re: [Q]: How to sort first abc, then 123?

So I'll have less posts to read?

--
Fort Worth, Texas

## Re: [Q]: How to sort first abc, then 123?

HEB wrote:

> I want the sorted output like: (first grouped aphabetically, then sorted by
number)
>

not the shortest, but it seems to work:

!/usr/bin/perl -w

use strict;

my @a=qw(and1 and12 and45 and7 and9 ana1 and bia 23 12);
@a=(@a,map @a);

print join("\n", sort ss @a);

sub ss {
my \$a1='';my \$a2=0;
if (\$a=~/(\D*)(\d*)/) {
\$a1=\$1;\$a2=\$2;
}
my \$b1='';my \$b2=0;
if (\$b=~/(\D*)(\d*)/) {
\$b1=\$1;\$b2=\$2;
}
\$a2=0 if not \$a2;
\$b2=0 if not \$b2;
return (\$a1 cmp \$b1 || \$a2 <=> \$b2);
}

best,
peter

--
http://www2.goldfisch.at/know_list

## Re: [Q]: How to sort first abc, then 123?

HEB wrote:
>
> my @oldlist =
> (And64,And1,And16,And8,And24,And32,Nand64,Nand1,Nand16,Nand8,Nand24,Nand32);

> I want the sorted output like: (first grouped aphabetically, then sorted by
number)

Another way:

my @sorted = map \$_->[0], sort {
\$a->[1] cmp \$b->[1]
||
\$a->[2] <=> \$b->[2]
} map [ \$_, /(\D*)(\d*)/ ], @oldlist;

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

## Re: [Q]: How to sort first abc, then 123?

[A complimentary Cc of this posting was sent to
HEB
> my @sort1 = sort by_abc123     @oldlist;
> sub by_abc123 {
>         \$at = \$a;
>         \$bt = \$b;
>         \$aa = \$at =~ s/([A-Za-z_]*)([\d]*)/\$1/;
>         \$ba = \$bt =~ s/([A-Za-z_]*)([\d]*)/\$1/;
>         \$a1 = \$a =~ s/([A-Za-z_]*)([\d]*)/\$2/;
>         \$b1 = \$b =~ s/([A-Za-z_]*)([\d]*)/\$2/;
>         ( \$aa cmp \$ba || \$a1 <=> \$b1 );
> }

Not tested:

map {s/~//, \$_} sort map @data;

Hope this helps,
Ilya

## Re: [Q]: How to sort first abc, then 123?

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Ilya Zakharevich
> Not tested:
>
>   map {s/~//, \$_} sort map @data;

Oups, too quick fingers:

map {s/~//, \$_} sort map {s/^(?=[~\d])/~/, \$_} @data;

Sorry,
Ilya

## Re: [Q]: How to sort first abc, then 123?

* Ilya Zakharevich wrote:
> >
> > Not tested:
>
>     map {s/~//, \$_} sort map {s/^(?=[~\d])/~/, \$_} @data;

I have just tried it out but unfortunately it doesn't work as expected
(Perl 5.8.6). I also cannot recognize what you have had in mind. Perhaps
you can describe it briefly.

But your idea takes me to another one with the sort() call without a
sorting function of its own, though. You can convert the trailing
numbers into characters before sorting:

@data = map { s/(.)\$/ord\$1/e; \$_ }
sort map { s/(\d*)\$/chr\$1/e; \$_ } @data;

regards,
fabian

## Re: [Q]: How to sort first abc, then 123?

[A complimentary Cc of this posting was sent to
Fabian Pilkowski
> > > Not tested:
> >
> >     map {s/~//, \$_} sort map {s/^(?=[~\d])/~/, \$_} @data;
>
> I have just tried it out but unfortunately it doesn't work as expected

Tested:

map {s/^~//; \$_} sort map {s/^(?=[~\d])/~/; \$_} @data;

(one '^' inserted, and commas replaced by semicolon).

I have no idea why commas lead to a wrong result - IIRC, if it mixed
map BLOCK DATA with map EXPR, DATA, it would give a syntax error...

Hope this helps,
Ilya

## Re: [Q]: How to sort first abc, then 123?

Ilya Zakharevich (nospam-abuse@ilyaz.org) wrote on MMMMCCXIII September
:)  [A complimentary Cc of this posting was sent to
:)  Fabian Pilkowski
:) > > > Not tested:
:) > >
:) > >     map {s/~//, \$_} sort map {s/^(?=[~\d])/~/, \$_} @data;
:) >
:) > I have just tried it out but unfortunately it doesn't work as expected
:)
:)  Tested:
:)
:)    map {s/^~//; \$_} sort map {s/^(?=[~\d])/~/; \$_} @data;
:)
:)  (one '^' inserted, and commas replaced by semicolon).
:)
:)  I have no idea why commas lead to a wrong result - IIRC, if it mixed
:)  map BLOCK DATA with map EXPR, DATA, it would give a syntax error...

The statement inside the map block is in list context, so if you have

map {s/^(?=[~\d])/~/, \$_} @data

the substitution is done in list context, and its result will be part
of the result of the map.

Replacing the comma with ; means that only \$_ is returned.

Abigail
--
perl -Mstrict -we '\$_ = "goto A.print chop;\n=rekcaH lreP rehtona tsuJ";A1:eval'

## Re: [Q]: How to sort first abc, then 123?

* Ilya Zakharevich wrote:
>
> Tested:
>
>   map {s/^~//; \$_} sort map {s/^(?=[~\d])/~/; \$_} @data;
>
> (one '^' inserted, and commas replaced by semicolon).

Sorry, but I can't get it out yet. If the unsorted @data is like

my @data = qw( And64 And8 Nand64 Nand8 );

the desired output should be

qw( And8 And64 Nand8 Nand64 );

Within your first map (therewith I mean the back one), you searched for
those strings beginning with a tilde or a number, to insert a new tilde
in front of them. For the given strings this will never match ;(

Hence the sort call is doing its standard string comparison which won't
change the data (the four element above are sorted this way already).
understandable.

>
> Hope this helps,

Even now I don't understand how adding tildes could be used to solve
this sorting problem.

regards,
fabian

## Re: [Q]: How to sort first abc, then 123?

[A complimentary Cc of this posting was sent to
Fabian Pilkowski
> >   map {s/^~//; \$_} sort map {s/^(?=[~\d])/~/; \$_} @data;

> Sorry, but I can't get it out yet. If the unsorted @data is like
>
>     my @data = qw( And64 And8 Nand64 Nand8 );
>
> the desired output should be
>
>     qw( And8 And64 Nand8 Nand64 );

This is not the problem in the Subject.  Since you want 8 before 64,
this is not easily made with substitutions followed by lexicographical
sort (unless you encode 64 as chr(64)).  So compexities associated
with conversion to arrays are hardly avoidable.

Yours,
Ilya

P.S.  I though you wanted 123 to be sorted after abc.

## Re: [Q]: How to sort first abc, then 123?

Hi, Sinan, tad, Peter, Gunnar, IIya and Fabian:

You guys' response time is fast... :)

I'm not sure if the X-No-archive really works, but
if it does, I hope I can keep my opinion to myself. :)

HEB

> Hi, All:
>
>
> my @oldlist =
> (And64,And1,And16,And8,And24,And32,Nand64,Nand1,Nand16,Nand8,Nand24,Nan
> d32);
>
> I want the sorted output like: (first grouped aphabetically, then
> sorted by number)
> ....

## Re: [Q]: How to sort first abc, then 123?

> Hi, Sinan, tad, Peter, Gunnar, IIya and Fabian:

Hello.

You are welcome.

> You guys' response time is fast... :)

abiding by them. So, next time, please do not top-post, and please quote
appropriately.

> I'm not sure if the X-No-archive really works, but
> if it does, I hope I can keep my opinion to myself. :)

I am not really sure what you mean by this, but sounds selfish.

Sinan.

## Re: [Q]: How to sort first abc, then 123?

HEB wrote:
>
> I'm not sure if the X-No-archive really works, but
> if it does, I hope I can keep my opinion to myself. :)
>

So then you should not post to usenet, cause its a public place.
Especially in technical groups the monopolistic google-archive of all
usenet-postings is a very important thing, so people dont need to

p

--
http://www2.goldfisch.at/know_list

## Re: [Q]: How to sort first abc, then 123?

Just in case readers didn't pick it up earlier in this thread:

Less people will see your question if you set X-No-archive.

I choose to not help folks who are unwilling to go on record,
so my newsreader deletes their posts automatically for me.

I never even see posts that have that header set.

Also, trolls often set it so that evidence of their trollage isn't
there to give up the game when they take it to a new venue.

--
Fort Worth, Texas

## Re: [Q]: How to sort first abc, then 123?

> I want the sorted output like: (first grouped aphabetically, then
> sorted by number)

Perhaps the Sort::Naturally module
(http://search.cpan.org/~sburke/Sort-Naturally-1.02/lib/Sort/Naturally.pm)
does what you want.

dha
--
And you know, you can have all the good audio gear in the world, but
unless you make a point of asking the informant to not eat a Snickers
while he's talking, it's all for naught.        - TorgoX, #perl

## Re: [Q]: How to sort first abc, then 123?

I often use this

sub alnum_sort {
return map \$_->[1], sort { \$a->[0] cmp \$b->[0]} map {
my \$str = uc;
\$str =~ s|(0*)(\d+)|pack("AN/A*N", "0", \$2, length(\$1))|eg;
[\$str, \$_];
} @_;
}

which may or may not be what you want.