Forum Moderators: coopster & phranque

Message Too Old, No Replies

Sorting by numeric parts of a string

how do u sort an array according to the numeric part of a string

         

Sheffielder

7:17 pm on Jul 21, 2004 (gmt 0)

10+ Year Member



Hi all...
I wish to sort an array containing various details of a number of documents such that they are sorted in the order of the numerical part of the $file_code for each document. The details will then be displayed in an html table, the first column of which contains the $file_code for each document.

The file codes are of the form
abc01, abc02, abc03, ... abc10, abc11, ...abc99, abc100, abc 101 etc.
Each document in any given sort procedure has the same "abc" (and the "abc" is a known string).
ie the problem with a normal string sort is that the numerical part is just two digits long for 01 to 99 but three digits long for 100 onwards.

What is the most efficient means of doing this sort?

many thanks in advance...

Flash

8:11 am on Jul 22, 2004 (gmt 0)

10+ Year Member



I discovered a superb perl module for doing just this - sorting arrays in a comma-deliminated record. So you can sort the entire array on the 3rd or 4th or whatever field of each record, and sort reverse/numerical/alpha etc.

[search.cpan.org ]

use Sort::Fields;

And sort like so:
@newsorted = fieldsort '\¦', ['-21n'], @db;

(Reverse sort with delimiter '¦', the 21st Field, n for numerical (so 8 comes before 81) from array @db to array @newsorted)