Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Sort routine

by RMGir (Prior)
on Sep 18, 2002 at 12:02 UTC ( [id://198802]=note: print w/replies, xml ) Need Help??


in reply to Sort routine

You've got a few options. You can change how you store your data, storing your time values with 0 padding, like this:
sub makeEvent { sprintf("%08d\t%s",@_); } @event_queue = (makeEvent($time,"Event 1"), makeEvent($time,"Event 5") +, makeEvent($time,"Event 5")); # now this works fine! @event_queue=sort @event_queue;
That's actually a variant of a technique called the Guttman Rossler Transform; in the GRT you'd encode your times just before sorting, and decode them after you're done. But if you can just keep them in sortable form all along, life is simpler.

The other alternative is the Schwartzian Transform, which is a fraction less efficient, but will still do the job very well...

@event_queue = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, (split/\t/)[0]] } @event_queue;
The trick here is that the time values are extracted, and then compared as integers to get things in the right order. Then the strings that were associated with those integers are returned by the top map call.
--
Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://198802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found