fanfavorite Posted April 1, 2009 Share Posted April 1, 2009 Is there a way to do a comparison sort for more than 2 values? I use uasort to compare 2 values, but in some cases need to compare 3+. Can someone help me get on track? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152100-sort-compare-more-than-2-values/ Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 You just have to code the user function to do it. I have never seen it done. But I am sure it is possible to do, perhaps post your sort function code and maybe a before and end example of the data/sorted you want returned. Quote Link to comment https://forums.phpfreaks.com/topic/152100-sort-compare-more-than-2-values/#findComment-798794 Share on other sites More sharing options...
fanfavorite Posted April 1, 2009 Author Share Posted April 1, 2009 I'll make it simple. If I get the process, I can figure out how to incorporate mine better. Here is the simple version right now: function Compare_standings($ar1, $ar2){ if ($ar1['pts']<$ar2['pts']) { return 1; } else if ($ar1['pts']>$ar2['pts']) { return -1; } if (in_array($ar1['id'],$ar2['teamsbeat'])) { return 1; } elseif (in_array($ar2['id'],$ar1['teamsbeat'])) { return -1; } return 0; } What I would like to do is if there are more than 2 with the same amount of "pts" then check all 3: function Compare_standings($ar1, $ar2, $ar3){ if ($ar1['pts']<$ar2['pts']) { return 1; } else if ($ar1['pts']>$ar2['pts']) { return -1; } if ($ar3['pts'] == $ar1['pts']) { if (in_array($ar1['id'],$ar2['teamsbeat']) AND in_array($ar3['id'],$ar2['teamsbeat'])) { return 1; } elseif (in_array($ar2['id'],$ar1['teamsbeat']) AND in_array($ar3['id'],$ar1['teamsbeat'])) { return -1; } } else { if (in_array($ar1['id'],$ar2['teamsbeat'])) { return 1; } elseif (in_array($ar2['id'],$ar1['teamsbeat'])) { return -1; } } return 0; } Not sure exactly how I should be structuring because not sure how the compare works with more than 2 values, but this should show what I am trying to do. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/152100-sort-compare-more-than-2-values/#findComment-798816 Share on other sites More sharing options...
fanfavorite Posted April 2, 2009 Author Share Posted April 2, 2009 Can anyone shed some light or perhaps point me in the right direction? Really stuck here. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152100-sort-compare-more-than-2-values/#findComment-799242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.