xwishmasterx Posted December 8, 2013 Share Posted December 8, 2013 Hello I am having issues trying to sort some some results. example results from table: 30/1 8/2 12/1 12/2 How can I sort these to make value before "/" go first and if first value are the same the the value after "/" be first: 30/1 12/2 12/1 8/2 All ideas appreciated Quote Link to comment https://forums.phpfreaks.com/topic/284619-sorting-strings/ Share on other sites More sharing options...
requinix Posted December 8, 2013 Share Posted December 8, 2013 Use usort with a function like function($a, $b) { list($a1, $a2) = explode("/", $a); list($b1, $b2) = explode("/", $b); return ($a1 != $b1 ? $a1 - $b1 : $a2 - $b2); } Quote Link to comment https://forums.phpfreaks.com/topic/284619-sorting-strings/#findComment-1461642 Share on other sites More sharing options...
hitman6003 Posted December 8, 2013 Share Posted December 8, 2013 Also check out the natsort function. Alternatively, you could brute force it with some loops... foreach ($results as $result) { list($a, $b) = explode("/", $result); $data[$a][] = $b; } foreach (ksort($data) as $key => $values) { sort($values); foreach (sort($value) as $x) { print $key . "/" . $x; } }Not exactly elegant though. Quote Link to comment https://forums.phpfreaks.com/topic/284619-sorting-strings/#findComment-1461650 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.