Sort Array

Discussion in 'PHP' started by you-cubez, May 3, 2009.

  1. #1
    Hi,

    I have an array (PHP) with 2 values:

    E.g.
    1, apple
    30, banana
    23, melon

    I want to sort the array by the integer before the comma and string... any ideas?

    Regards,
     
    you-cubez, May 3, 2009 IP
  2. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
  3. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I have tried that... but I need a function that splits the record and uses the first value to sort.
     
    you-cubez, May 3, 2009 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    hmm, do you have an array or a string? based on your first post you mentioned an array
     
    creativeGenius, May 3, 2009 IP
  5. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #5
    yes (sorry I mean split the data) - it is an array that contains data in the following format:

    integer, name
    E.g. 25, banana

    So, what I need to do it sort this array by the value of the integer before the comma and name.

    So let's say the array contained the following:

    25, banana
    160, apple,
    4, melon
    2009, grapes

    I need it to be sorted into the following:
    2009, grapes
    160, apple,
    25, banana
    4, melon

    (DESC order)
     
    you-cubez, May 3, 2009 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    
    $x=0;$newArray = array();
    foreach($firstArray as $item)
    {
        list($newArray[$x][0],$newArray[$x][1]) = explode(',',$item);
        $x++;
    }
    
    PHP:
    newArray is now sortable.

    Peace,
     
    Barti1987, May 3, 2009 IP
  7. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Hi azizny,

    Apologies for the delay...

    Would you mind showing me how to integrate into my existing script? Ideally, I want to arrange $total by the 1st value.

    
    while( $ratings2 = mysql_fetch_assoc( $ratings ) )
    { 
            $points = $ratings2['points'];		
            $name = $ratings2['name'];
            $total.= "Points: {$p}, Name: {$n}";
    }
    
    PHP:
    Many Thanks!
     
    you-cubez, May 19, 2009 IP
  8. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #8
    
    while( $ratings2 = mysql_fetch_assoc( $ratings ) )
    {
            $myArray[$ratings2[points]] = $ratings2['name'];
    }
    sort($myArray);
    foreach($myArray as $point => $name){ $total.= "Points: {$point}, Name: {$name}";}
    
    PHP:
    not test.

    Peace,
     
    Barti1987, May 19, 2009 IP
  9. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #9
    With a LIMIT of 50, it only seems to show the first 13 rows and seems to order by array integer.

    Eg.
    Points: 0, Name: Namea
    Points: 1, Name: Nameb
    Points: 2, Name: Namec
    Points: 3, Name: Named

    If you're interested, PM for IM (MSN/AIM/Skype etc...) - I'll throw some $$ in for the help.
     
    you-cubez, May 19, 2009 IP
  10. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #10
    Maybe some points are the same, try this:

    
    $myArray = array();
    while( $ratings2 = mysql_fetch_assoc( $ratings ) )
    {
            array_push($myArray,array($ratings2['points'],$ratings2['name']));
    }
    sort($myArray);
    foreach($myArray as $item){ $total.= "Points: {$item[0]}, Name: {$item[1]}";}
    
    PHP:
    Tested.

    Peace,
     
    Barti1987, May 19, 2009 IP
  11. you-cubez

    you-cubez Active Member

    Messages:
    842
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    60
    #11
    Perfect! Working... although, any way to change to Dsc instead of Asc?

    PS. PM paypal ad.
     
    you-cubez, May 19, 2009 IP
  12. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #12
    Try different sort methods like arsort.

    Check PHP documentation by clicking on the sort link in the code.

    Peace,
     
    Barti1987, May 19, 2009 IP