FooKelvin Posted October 25, 2015 Share Posted October 25, 2015 Hi, i would like to sort the total and name with different type of sorting. uasort($data, function($a, $b) { $total = $b['total'] - $a['total']; //SORT BY DESC if($total === 0) { return strcmp($b['emp'], $a['emp']); // THIS line need to sort by ASC } return $total; }); Sort Total by DESC at the same time, sort emp BY ASC Quote Link to comment https://forums.phpfreaks.com/topic/298812-how-to-have-2-field-sorting-in-php/ Share on other sites More sharing options...
Solution Barand Posted October 25, 2015 Solution Share Posted October 25, 2015 Just swap the $a and $b round uasort($data, function($a, $b) { $total = $b['total'] - $a['total']; //SORT BY DESC if($total === 0) { return strcmp($a['emp'], $b['emp']); // THIS line now sorts by ASC } return $total; }); Quote Link to comment https://forums.phpfreaks.com/topic/298812-how-to-have-2-field-sorting-in-php/#findComment-1524276 Share on other sites More sharing options...
FooKelvin Posted October 26, 2015 Author Share Posted October 26, 2015 Thanks Barand! Quote Link to comment https://forums.phpfreaks.com/topic/298812-how-to-have-2-field-sorting-in-php/#findComment-1524327 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.