Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Sort string according to numbers in it

by jeffa (Bishop)
on Apr 30, 2015 at 17:58 UTC ( [id://1125299]=note: print w/replies, xml ) Need Help??


in reply to Sort string according to numbers in it

You aren't very clear on precisely what your output should be. It sounds like you want an ascending sort based on the number next to the name. This will do that:

use strict; use warnings; my $str = 'Lee Morgan : 20 Clifford Brown : 3 Freddie Hubbard : 6 '; my @data = map {[ split /\s+:\s+/, $_]} split /\n/, $str; for (sort { $a->[1] <=> $b->[1] } @data) { print join( ' : ', @$_ ), $/; }

You can break the string up with split into a 2D array and then sorting by that number becomes much easier.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Sort string according to numbers in it
by Alexander75 (Novice) on Apr 30, 2015 at 18:19 UTC
    Jeffa, thanks you so much so much so much. I just put on a "reverse" because I needed from bigger to smaller, and now that gives me exactly what I wanted. I will study this little code and keep it in mind. Thank you

      If you need descending order, rather than wastefully filtering the sorted results through reverse you can instead just swap $a and $b:

      for (sort { $b->[1] <=> $a->[1] } @data) { print join( ' : ', @$_ ), $/; }
      And since we are on the topic, be sure and check out the Schwartzian Transform.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        ... wastefully filtering the sorted results through reverse ...

        Somewhere/when, I remember seeing a note to the effect that the Perl compiler optimizes the instruction sequence  reverse sort so that a "native" reversed or descending sort occurs; i.e., there is no difference in time/space complexity between reversed and non-reversed sort. Looking through the docs just now does not turn up anything about this for me. However, experimentation (with Perl version 5.14 only) shows it is true, at least in the default sort (i.e., no comparison block) case:

        c:\@Work\Perl>perl -wMstrict -le "use List::Util qw(shuffle); ;; print 'shuffling...'; my @rra = shuffle 1 .. 10_000_000; ;; print 'sorting...'; my (@sorted, $start, $dur_asc, $dur_des); ;; $start = time; @sorted = sort @rra; $dur_asc = time - $start; ;; @sorted = (); ;; $start = time; @sorted = reverse sort @rra; $dur_des = time - $start; ;; print 'ascending (default sort): ', $dur_asc; print 'descending (reverse sort): ', $dur_des; ;; sub pra { print qq{@{ $_[0] }[ 0 .. 9 ] @{ $_[0] }[ -10 .. -1 ]}; } " shuffling... sorting... ascending (default sort): 57 descending (reverse sort): 53
        Calls to  pra() at various points against the  @rra and  @sorted arrays show expected results: at least the first and last few elements of these arrays are always as expected.

        One oddity: The second sort performed is always just a few seconds faster than the first. This is true regardless of whether the reversed sort is performed first or second. I attribute this small but consistent speed difference to the Perl interpreter having already allocated an internal, intermediate array for the first sort that still exists at the time of the second sort and so does not need re-allocation, thus saving a bit of time. However, I can't see how to prove this speculation. Any thoughts?

        Update: Limited experimentation with version 5.8.9 shows essentially the same behavior.


        Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found