#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Sort mutlidimensional array with certain values always at the last


    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 order
    newScore1 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 order
    newScore1 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,453
    Rep Power
    594
    You will have to split the array with a custom function. Have the function loop through the array and extract those members whose score1 is 0 or DNF. The resulting array can then be sorted in ascending or descending order. After the sort, append the 0 and DNF members to the sorted array.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0
    Originally Posted by gw1500se
    You will have to split the array with a custom function. Have the function loop through the array and extract those members whose score1 is 0 or DNF. The resulting array can then be sorted in ascending or descending order. After the sort, append the 0 and DNF members to the sorted array.
    Thanks for your reply.
    Regards
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    You could also use usort and build these special cases into your comparison function.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo