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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting question 1

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I always use something like this to sort an array of a database:

@array = sort {$a <=> $b} @array;

A single array element (record) looks like this:

number|element2|element3|element4|

Is there a way to directly sort the array on element4?

Regards,

Ron
 
The most obvious approach to this would be to use something akin to a Schwartzian Transform. I'm not sure if it's the most efficient way, however.

The code goes like this:
Code:
#!/usr/bin/perl

@unsorted = ('a|b|c|d|','a|b|c|b|','a|b|c|e','a|b|c|a|');

@sorted = map { $_->[0] } 
  sort { $a->[1] cmp $b->[1] } 
    map { 
      my @s = split /\|/; 
      [$_, pop @s]
    } @unsorted;

Hope this helps,

brendanc@icehouse.net
 
Thanks for helping me with this Sophisticate.

I am not sure how your solution is working but it doesn't have the right outcome. The true element I want to sort at is the contents between the 19th and the 20th &quot;|&quot; (numbers).

I tried all sort of things but I can't get it to work.

Safra
 
Try something like this:
Code:
@array = sort MySortRtn @array;
...
sub MySortRtn {
   my @a = split('|', $a);
   my @b = split('|'. $b);
   return $a[19] <=> $b[19];
}
You may have to adjust the subscripts in the sort routine, but that should do the job.

Note that MySortRtn is a BARE WORD! Do NOT put an & in front of it!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Safra,

Note this line:
[$_, pop @s]

This is the line that returns an array to the sort command (and the first map) with the original element value, and the value you want to sort on. pop @s simply returns the last element of the array created by split//. You can pick any of the elements in @s by replacing pop @s with $s->[elementnumber].

Hope this helps,

brendanc@icehouse.net
 
sophisticate,

I changed pop@s in $s->[19] now all elements in the returned array are the same (the first of the original array)?

tsdragon,

I tried your suggestion. First it didn't work (the array looked the same as the original). But after changing '|' into /\|/ it did work. Thanks!

Ron

 
My mistake.. to attain the nth position in the array, you must use one of the following syntaxes (keep in mind that the array's zero indexed):

@s->[element] # oops!
$s[element]

Sorry
 
Oops to me too! I forgot that the pipe symbol has special meaning in a regular expression and has to be escaped! I'm glad you were able to catch that error.

Sorry :~/ Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top