We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Sorting

rafistern
rafistern asked
on
Medium Priority
197 Views
Last Modified: 2010-03-05
I want to sort an array of arrays.

I use the line

sort byName @records;

where byName is defined as

sub byName{
      return $records[$a]->[$SITENAME] cmp $records[$b]->[$SITENAME];
}

Please explain why this doesn't work.
Comment
Watch Question

Commented:
Just to clarify:  Do you want each subarray sorted within itself or are you trying to do some sort of sorting between the subarrays?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
try
  @records[ sort byName (0..$#records)];
or
  sort {$a->[$SITENAME] cmp $b->[$SITENAME]} @records;

Author

Commented:
arnhold,
I have a set of arrays which contain dat that I read from a flat file database. I want to sort the arrays so that they will be in ascending order sorted by a key field (the name of the item to which all the data in the record refers).

ozo,
I will try your suggestions.

Thanks,
Rafi

Author

Commented:
ozo,
It doesn't work. Anyone know what the correct syntax is for this operation? I could always write my own sorting routine.. but it's a shame not to use the existing function "sort".
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
What doesn't work.
How is @records defined?

Author

Commented:
I just found the answer myself.
@records= was missing at the beginning of the line!
What a stupid mistake!!

Thanks, guys.

Author

Commented:
P.S.
ozo, I will give you points for your answer because your syntax was correct although you didn't find my other mistake. Post me a blank answer.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
> you didn't find my other mistake
How could I, when you didn't tell me you wanted to replace @records with the sorted array?
That's why I asked what wasn't working.

Author

Commented:
It's not really worth arguing about but the line which I posted:

sort byName @records;

has got to be just one of the most useless lines of code as it doesn't return anything. Thanks for the correct syntax for the sorting function. It's now working fantastically.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
If that's the complete statement, yes, and perl -w will even warn you about that.
But besides assigning it to @records, you might have wanted to return it from a sub, or assign it to something else, or print it, or map it, or foreach over it, or pass it to another function...
Anyway, I'm glad it's working for you now.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.