[Please don't top posting]
Radek G. wrote:
> Uzytkownik "Gunnar Hjalmarsson" <> napisal w
> wiadomosci news:wc5Wb.82194$...
>> Radek G. wrote:
>>> I must implemend sorting in perl (that's not too dificult
)
>>> but...i must sort some array with grouping (like in sql).
>>> I meen... For Example I have array with first and last names:
>>> @names= ('a a','a b','a c',' b b', 'b d' ). And i must sort it
>>> first by first name from 'z' to 'a' and by last name from 'a' to
>>> 'z'.
>>
>> Would you mind checking out the docs for the sort() function?
> I've check...smart man.
> So if you are so smart - can you do it with one sort?
That was not apparent from your first posting. Typically people coming this
way do not check the docs first.
Furthermore, if you would have checked the docs, then I would have expected
your question to be different. I would have expected you to ask about the
comparison function for you unusual requirements.
Furthermore "perldoc -q sort" has even an example that almost fits your
bill:
If you need to sort on several fields, the following paradigm is
useful.
@sorted = sort { field1($a) <=> field1($b) ||
field2($a) cmp field2($b) ||
field3($a) cmp field3($b)
} @data;
Now, all you have to do is define field1() and field2() and watch out for
the right arrangement of arguments in order to get the lesser-values-first
versus higher-value-first sequence.
This may not be the most efficient solution but it'll do the job. To
increase efficiency just split $a nd $b once and then use the individual
parts in the actual comparison.
> I don't.
I'm sure you can.
jue
So, I guess you have problems defining a function, which for any to
arguments (from the set of