Click here to get back home

sorting index-15, index-9, index-110 "the human way"?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski 03-04-2008
Get Chitika Premium
Posted by Frank Seitz on March 4, 2008, 8:35 am
Please log in for more thread options
Tomasz Chmielewski wrote:
>
> What do you guys use to sort such things?

$ perldoc -q 'sort an array'

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Frank Seitz on March 4, 2008, 8:47 am
Please log in for more thread options
Tomasz Chmielewski wrote:
>
> What do you guys use to sort such things?

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

$ perldoc -q 'sort an array'

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Abigail on March 4, 2008, 11:07 am
Please log in for more thread options
_
Tomasz Chmielewski (tch@nospam.syneticon.net) wrote on VCCXCIX September
@@ Let's say I have an array consisting of:
@@
@@ some-name-10
@@ some-name-9
@@ some-name-102
@@ some-name-89
@@
@@ I would like to sort it the "human way", with the result:
@@
@@ some-name-9
@@ some-name-10
@@ some-name-89
@@ some-name-102
@@
@@
@@ What would be the suggested way to do it?
@@
@@ I would use 'split', but "some-name" is not static, and can be
@@ "some-other-name" or just "name" a task later.
@@
@@ For the same reason, using 'substr' won't work.
@@
@@ Unless I write a subroutine to it.
@@
@@
@@ What do you guys use to sort such things?


Well, that depends. Your specification, or rather lack of specification,
allows multiple interpretations. If you have:

sort-name-20
sort-name-7
name-15
name-8

do you want:

name-8
name-15
sort-name-7
sort-name-20

or

sort-name-7
name-8
name-15
sort-name-20

?



Abigail
--
perl -we '$_ = q ;4a75737420616e6f74686572205065726c204861636b65720as;;
for (s;s;s;s;s;s;s;s;s;s;s;s)
{s;(..)s?;qq qprint chr 0x$1 and 1 ssq;excess;}'

Posted by Tomasz Chmielewski on March 4, 2008, 12:01 pm
Please log in for more thread options
Abigail schrieb:
> _
> Tomasz Chmielewski (tch@nospam.syneticon.net) wrote on VCCXCIX September
> @@ Let's say I have an array consisting of:
> @@
> @@ some-name-10
> @@ some-name-9
> @@ some-name-102
> @@ some-name-89
> @@
> @@ I would like to sort it the "human way", with the result:
> @@
> @@ some-name-9
> @@ some-name-10
> @@ some-name-89
> @@ some-name-102
> @@
> @@
> @@ What would be the suggested way to do it?
> @@
> @@ I would use 'split', but "some-name" is not static, and can be
> @@ "some-other-name" or just "name" a task later.
> @@
> @@ For the same reason, using 'substr' won't work.
> @@
> @@ Unless I write a subroutine to it.
> @@
> @@
> @@ What do you guys use to sort such things?
>
>
> Well, that depends. Your specification, or rather lack of specification,
> allows multiple interpretations. If you have:
>
> sort-name-20
> sort-name-7
> name-15
> name-8
>
> do you want:
>
> name-8
> name-15
> sort-name-7
> sort-name-20
>
> or
>
> sort-name-7
> name-8
> name-15
> sort-name-20

The latter. And the "name" would always be the same (it would only
change in the next iteration; but for a given iteration, all names would
be the same).

So essentially, as I wrote:

some-name-10
some-name-9
some-name-102
some-name-89

with the result:

some-name-9
some-name-10
some-name-89
some-name-102


But I think "sub extract" discussed earlier is simple enough for this
case, and I used it.



--
Tomasz Chmielewski
http://wpkg.org

Similar ThreadsPosted
human nature of perl (new operators etc) November 11, 2004, 11:37 am
Sorting November 28, 2004, 10:43 pm
sorting July 7, 2005, 5:04 am
Sorting June 7, 2007, 4:44 am
Sorting an Array by Another January 21, 2005, 12:56 pm
sorting a structure May 5, 2005, 3:22 pm
Hash Sorting June 14, 2005, 2:49 pm
table sorting May 3, 2006, 5:58 pm
Sorting EBCDIC December 15, 2006, 11:48 am
Sorting Hash by Value and Key May 17, 2007, 9:57 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap