Jump to content

Sorting in php


stb74

Recommended Posts

Hi

 

I am trying to create a function that will enable me to sort a soccer league table.  Sort will be on several fields

 

Points

GoalDiff

GoalsFor

 

I found this example that I though might help me find a solution but it gives an error

 

Parse error: parse error, unexpected T_NEW in D:\XAMPP Web\xampp\htdocs\nifootball\sort.php on line 29

 


function sortDataSet(&$dataSet) {
    $args = func_get_args();
    $callString = 'array_multisort(';
    $usedColumns = array();
    for($i = 1, $count = count($args); $i < $count; ++$i) {
        switch(gettype($args[$i])) {
            case 'string':
                $callString .= '$dataSet[\''.$args[$i].'\'], ';
                array_push($usedColumns, $args[$i]);
                break;
            case 'integer':
                $callString .= $args[$i].', ';
                break;
            default:
                throw new Exception('expected string or integer, given '.gettype($args[$i]));
        }
    }
    foreach($dataSet as $column => $array) {
        if(in_array($column, $usedColumns)) continue;
        $callString .= '$dataSet[\''.$column.'\'], ';
    }
    eval(substr($callString, 0, -2).');');
}

/* example usage */
    /* example usage */
$latestPost = array(
    'team' => array(
        '0',
        '1',
        '2',
        '4',
        '5'
    ),
    'name' => array(
        'team1',
        'team2',
        'team3',
        'team4',
        'team5'
    ),
    'date' => array(
        '10',
        '8',
        '12',
        '20',
        '4'
    )
);
sortDataSet($latestPost, 'name', SORT_DESC, SORT_STRING, 'team', SORT_DESC, SORT_STRING);
echo '<pre>';
print_r($latestPost);
echo '</pre>';
?> 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/64254-sorting-in-php/
Share on other sites

Its easier than you think.  The trick is this

1) Execute a query

2) In a while loop make a multi dimensional array of said data i.e ($data[$i]['Name'] $data[$i]['Stats'])

3) In same while loop create a single level array of each stat you wish to sort by i.e ($name = array(); $name[$i] = $row['Name'])

4) User sort or reverse sort to get your sorting parms in correct order.

5) Then apply sorting from a top down method (i.e first sorted will be last one in order)

 

The sorting is the tricky part, but php has a function for it. its called multi sort read:

http://us2.php.net/manual/en/function.array-multisort.php

and you should understand it pretty easily.

 

once you done that say print_r($data) and then view the source to see the array sorted.

 

Example 261 on that page explains it very well

Link to comment
https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320925
Share on other sites

I as able to get it working by this.

 


function sortTable($array)
{
foreach ($array as $key => $row)
{
   $Points[$key] = $row['Points'];
   $GoalDiff[$key]  = $row['GoalDiff'];
   $GoalsFor[$key]  = $row['GoalsFor'];
   $teamName[$key]  = $row['teamName'];
}

array_multisort($Points, SORT_DESC, $GoalDiff, SORT_DESC, $GoalsFor, SORT_DESC, $teamName, SORT_STRING, $array);
return $array;
}

 

Is this way ok.

Link to comment
https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-320970
Share on other sites

try

<?php

$league = array (
        'team1' => array (
                    'Points'    => 8,
                    'GoalDiff'  => 3,
                    'GoalsFor'  => 4
                    ),
        'team2' => array (
                    'Points'    => 10,
                    'GoalDiff'  => 5,
                    'GoalsFor'  => 8
                    ),
        'team3' => array (
                    'Points'    => 10,
                    'GoalDiff'  => 6,
                    'GoalsFor'  => 8
                    ),
        'team4' => array (
                    'Points'    => 30,
                    'GoalDiff'  => 9,
                    'GoalsFor'  => 12
                    ),
        'team5' => array (
                    'Points'    => 10,
                    'GoalDiff'  => 6,
                    'GoalsFor'  => 9
                    )
);

uasort ($league, 'leaguesort');                     //  custom sort
echo '<pre>', print_r($league, true), '</pre>';     //  view result

function leaguesort ($a, $b)
{
    if ($a['Points'] == $b['Points'])
    {
        // sort by goaldiff
        if ($a['GoalDiff'] == $b['GoalDiff'])
        {
            // sort by goalsfor
            if ($a['GoalsFor'] == $b['GoalsFor']) return 0;
            return $a['GoalsFor'] > $b['GoalsFor'] ? -1 : 1;
        }
        return  $a['GoalDiff'] > $b['GoalDiff']  ? -1 : 1;
    }
    return  $a['Points'] > $b['Points']  ? -1 : 1; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/64254-sorting-in-php/#findComment-321045
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.