Using PHP to sort a list of text?

Discussion in 'PHP' started by DannyBat, Jan 8, 2007.

  1. #1
    Hey guys, I've been in need of something that will sort a list of text by the amount of characters on each line. I figured PHP could be used to code something like this, and would probably be much easier then making a program that does it. Does anyone know where I should start with this? I'm not exactly the greatest PHP coder, so if anyone can help me with this or point me in the right direction- I'd appreciate it a lot.

    An example of what I'd like it to do is; I paste the following text or even load a .txt file containing this text into a typing area.

    aaaa
    aaaaaa
    aaa
    aaaaaaaa
    aa
    a
    aaaaaaa

    and that gets sorted into

    a
    aa
    aaa
    aaaa
    aaaaaa
    aaaaaaa
    aaaaaaaa

    Hope that helps you guys figure out what I'm lookin' for.

    Any help would be appreciated
    -Danny
     
    DannyBat, Jan 8, 2007 IP
  2. Yeldarb

    Yeldarb Active Member

    Messages:
    209
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    Here's some psuedo code to help you visualize the solution

    Load the words into an array (hint: explode.. \n)
    Create a new array
    Loop until new array's length equals old array's length
    Start at length 0 go up
    When you find a string of the right length put it in the new array
    Print out new array
     
    Yeldarb, Jan 8, 2007 IP
  3. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP's sort function will do the trick. It sorts lowest to highest.

    
    $data = array("aaa", "aaaa", "a", "aaaaaaa");
    sort($data);
    foreach ($data as $key => $val) {
       echo "data[" . $key . "] = " . $val . "\n";
    }
    
    PHP:
     
    clancey, Jan 8, 2007 IP
  4. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #4
    probably better to do a file open, and then read the contents into the array :)
     
    daboss, Jan 8, 2007 IP
  5. thedark

    thedark Well-Known Member

    Messages:
    1,346
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    168
    Digital Goods:
    1
    #5
    it is easier to do it in C++, you have 2 options, import all words into an array, sort the array with any sorting method you know, but don't compare strings, but their lenghts, and then rewrite the array to the file.

    Other option is to make the sorting directly in the file, without using the array.
     
    thedark, Jan 8, 2007 IP
  6. DannyBat

    DannyBat Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for all your help guys, especially Yeldarb. He coded somethin' up for me.

    -Danny
     
    DannyBat, Jan 9, 2007 IP
  7. kohashi

    kohashi Well-Known Member

    Messages:
    1,198
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    140
    #7
    cs.ubc.ca/~harrison/Java/sorting-demo.html

    just some algorithms to look at for sorting data like this.
     
    kohashi, Jan 10, 2007 IP