Order a list by string size

Discussion in 'PHP' started by emitind, May 30, 2008.

  1. #1
    I had a quick search around Google and php.net but couldnt find a solution to ordering a long list of words into string length. So that the shortest is at the top. Is this possible with PHP? If so could someone please guide me as to how to acheive this.

    Thanks!
     
    emitind, May 30, 2008 IP
  2. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is the list in an array? Elaborate a bit more / give an example list :).
     
    CodyRo, May 30, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    
    function len($a, $b)
    {
        return strlen($a) - strlen($b);
    }
    usort($array, 'len');
    
    PHP:
    Knock yourself out ;)
     
    Danltn, May 30, 2008 IP
    emitind likes this.
  4. emitind

    emitind Peon

    Messages:
    567
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Fantastic, thanks Danltn :)
     
    emitind, May 30, 2008 IP