Perl: the Markov chain saw | |
PerlMonks |
Re: RFC: I rewrote a custom sort function, but not sure if it is the best way.by 7stud (Deacon) |
on Mar 03, 2013 at 02:15 UTC ( [id://1021482]=note: print w/replies, xml ) | Need Help?? |
Let's start with the premise that a sort routine shouldn't be longer than 10 lines. What can you do to comply with that restriction? It looks to me like you are doing a lot of array processing inside the sort routine. Why not process the whole array first, then apply your sort routine? The advantage of processing your array before sorting is that you only touch each element once, where sort touches the elements multiple times. The next question is: why do you think you need one sort routine to sort every conceivable array you might conjure up? A better way would be to do something like this:
Once again, by doing that you are moving some of the processing out of the sort routine by checking for the type before calling sort(). Next comes advanced sorting, i.e. efficient sorting. As davido mentioned, it is a great learning process to learn how to do a Schwartzian Transform or use an Orcish Manouvre in your sort routine. Okay, even though they sound like theorems you might learn about in a Theoretcial Physics class, they are quite easy to grok. But if you fail to fully grasp the above means for efficient sorting, not to worry--there is a module that will help you out called Sort::Maker.
In Section
Seekers of Perl Wisdom
|
|