Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Sort 1

Status
Not open for further replies.

rik1551

IS-IT--Management
Sep 14, 2001
10
GB
Hi,

I have an array of strings that I would like to sort by the second column which contains a hexadecimal value. The array looks typically as follows:

"/dev/rdisk/disk1 07:44 00075145 XP02"
"/dev/rdisk/disk2 05:23 00075145 XP02"
"/dev/rdisk/disk3 06:5f 00075145 XP02"
"/dev/rdisk/disk4 16:cc 00075145 XP02"
"/dev/rdisk/disk5 03:4a 00075145 XP02"

I'd like it to be sorted like:

"/dev/rdisk/disk5 03:4a 00075145 XP02"
"/dev/rdisk/disk2 05:23 00075145 XP02"
"/dev/rdisk/disk3 06:5f 00075145 XP02"
"/dev/rdisk/disk1 07:44 00075145 XP02"
"/dev/rdisk/disk4 16:cc 00075145 XP02"

thanks!
 
Just do a Schwartzian Transform

Code:
my @array = (
	"/dev/rdisk/disk1      07:44   00075145   XP02",
	"/dev/rdisk/disk2      05:23   00075145   XP02",
	"/dev/rdisk/disk3      06:5f   00075145   XP02",
	"/dev/rdisk/disk4      16:cc   00075145   XP02",
	"/dev/rdisk/disk5      03:4a   00075145   XP02",
);

@array = map {$_->[1]} 
	sort {$a->[0] cmp $b->[0]} 
	map {[/\s+(\S+)/, $_]} @array;

for (@array) {
	print "$_\n";
}

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top