Jump to content

sort mutlidimensional array with certain values always at the last


rupam_jaiswal
Go to solution Solved by Barand,

Recommended Posts

Hi,

Please excuse if similar issue has been posted earlier,

my array is like this

newScore1 is Array(    [0] => Array        (            [scoreid] => 12            [compid] => 4            [name] => andrew            [score1] => 4            [score2] => 122            [score3] => 12            [score4] => DNF            [rank1] => 5        )    [1] => Array        (            [scoreid] => 15            [compid] => 4            [name] => Ed            [score1] => DNF            [score2] => DNF            [score3] => 34            [score4] => DNF            [rank1] => 0        )    [2] => Array        (            [scoreid] => 14            [compid] => 4            [name] => andy            [score1] => 34            [score2] => 21            [score3] => 34            [score4] => 34            [rank1] => 4        )    [3] => Array        (            [scoreid] => 13            [compid] => 4            [name] => crusty            [score1] => 1            [score2] => 0            [score3] => 34            [score4] => 0            [rank1] => T2        )    [4] => Array        (            [scoreid] => 10            [compid] => 4            [name] => larry g            [score1] => 1            [score2] => 1            [score3] => 23            [score4] => 34            [rank1] => T2        )    [5] => Array        (            [scoreid] => 11            [compid] => 4            [name] => alex            [score1] => 0            [score2] => 33            [score3] => 0            [score4] => 44            [rank1] => 0        ))

I have to sort array with score1 values in ascending or descending order but if the value is 0 and DNF then it should always be at the bottom.

eg

priority_array = (0,'DNF')1) ascending ordernewScore1 is Array(    [3] => Array        (            [scoreid] => 13            [compid] => 4            [name] => crusty            [score1] => 1            [score2] => 0            [score3] => 34            [score4] => 0            [rank1] => T2        )    [4] => Array        (            [scoreid] => 10            [compid] => 4            [name] => larry g            [score1] => 1            [score2] => 1            [score3] => 23            [score4] => 34            [rank1] => T2        )    [0] => Array        (            [scoreid] => 12            [compid] => 4            [name] => andrew            [score1] => 4            [score2] => 122            [score3] => 12            [score4] => DNF            [rank1] => 5        )    [2] => Array        (            [scoreid] => 14            [compid] => 4            [name] => andy            [score1] => 34            [score2] => 21            [score3] => 34            [score4] => 34            [rank1] => 4        )    [1] => Array        (            [scoreid] => 15            [compid] => 4            [name] => Ed            [score1] => DNF            [score2] => DNF            [score3] => 34            [score4] => DNF            [rank1] => 0        )      [5] => Array        (            [scoreid] => 11            [compid] => 4            [name] => alex            [score1] => 0            [score2] => 33            [score3] => 0            [score4] => 44            [rank1] => 0        ))2) descending ordernewScore1 is Array(    [2] => Array        (            [scoreid] => 14            [compid] => 4            [name] => andy            [score1] => 34            [score2] => 21            [score3] => 34            [score4] => 34            [rank1] => 4        )    [0] => Array        (            [scoreid] => 12            [compid] => 4            [name] => andrew            [score1] => 4            [score2] => 122            [score3] => 12            [score4] => DNF            [rank1] => 5        )                [3] => Array        (            [scoreid] => 13            [compid] => 4            [name] => crusty            [score1] => 1            [score2] => 0            [score3] => 34            [score4] => 0            [rank1] => T2        )    [4] => Array        (            [scoreid] => 10            [compid] => 4            [name] => larry g            [score1] => 1            [score2] => 1            [score3] => 23            [score4] => 34            [rank1] => T2        )    [1] => Array        (            [scoreid] => 15            [compid] => 4            [name] => Ed            [score1] => DNF            [score2] => DNF            [score3] => 34            [score4] => DNF            [rank1] => 0        )    [5] => Array        (            [scoreid] => 11            [compid] => 4            [name] => alex            [score1] => 0            [score2] => 33            [score3] => 0            [score4] => 44            [rank1] => 0        ))

Thanks

Link to comment
Share on other sites

  • Solution

You just need a couple of custom sort functions, one for ascending and the other for descending sorts

usort($newScore1, 'mysortAsc');
// or
// usort($newScore1, 'mysortDesc');

function mysortAsc($a, $b )
{
    if (in_array($a['score1'], array(0,'DNF'))) return 1;
    if (in_array($b['score1'], array(0,'DNF'))) return -1;
    return $a['score1'] - $b['score1'];
}

function mysortDesc($a, $b )
{
    if (in_array($a['score1'], array(0,'DNF'))) return 1;
    if (in_array($b['score1'], array(0,'DNF'))) return -1;
    return $b['score1'] - $a['score1'];
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.