in reply to Sort Array by length of Value

There is an example in the documentation for the sort function of how to sort case insensitively. Adapting it to sort by length is trivial. You will need to use the length() function and the numeric comparison "spaceship" operator, <=>.

Replies are listed 'Best First'.
Re: Re: Sort Array by length of Value
by Emanuel (Pilgrim) on Oct 06, 2003 at 14:39 UTC
    too simple, i didn't see it...

    @combos = reverse sort {length(uc($a)) <=> length(uc($b))} @combos;

    sigh, thanks for pointing it out, i guess i was searching way too far.

    regards,

    Emanuel
      You don't need the reverse if you compare reversed values to begin with.
      @b = sort { length $b <=> length $a } @a;
      But then I also wonder if the order of the values _after_ sorting on length might be significant.   You might want to consider something like this:
      @b = sort { length $b <=> length $a || $a cmp $b } @a;
Re: Re: Sort Array by length of Value
by kesterkester (Hermit) on Oct 06, 2003 at 14:42 UTC
    E.g.,

    @arr = sort { length $b <=> length $a } @arr;