DamienRoche Posted October 14, 2008 Share Posted October 14, 2008 From the tutorials I've read this should be working. Here's the array and the sort: <?php Array ( [0] => 1,249 [1] => 33,182 [2] => 33,182 [3] => 9,333 [4] => 3,981,847 [5] => 9,149 [6] => 3,294 [7] => 17,928 [8] => 1,413 [9] => 2,994 ) $fig is as above; sort($fig); print_r($fig); ?> OUTPUT: <?php Array ( [0] => 1,249 [1] => 1,413 [2] => 17,928 [3] => 2,994 [4] => 3,294 [5] => 3,981,847 [6] => 33,182 [7] => 33,182 [8] => 9,149 [9] => 9,333 ) ?> The sort is having some effect but it's not sorting. NOTE: I've tried SORT_NUMERIC - the problem seems to be that it only sorts the first number before the comma (,).. Any help is much appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/128410-solved-having-issues-sorting-numbers-with-sort/ Share on other sites More sharing options...
Zhadus Posted October 14, 2008 Share Posted October 14, 2008 That's not technically sorting "numbers" there are commas. If you strip the numbers of those, it should sort properly. Link to comment https://forums.phpfreaks.com/topic/128410-solved-having-issues-sorting-numbers-with-sort/#findComment-665327 Share on other sites More sharing options...
DamienRoche Posted October 14, 2008 Author Share Posted October 14, 2008 So is there not a way to just sort the number and take into account the comma? Link to comment https://forums.phpfreaks.com/topic/128410-solved-having-issues-sorting-numbers-with-sort/#findComment-665328 Share on other sites More sharing options...
effigy Posted October 14, 2008 Share Posted October 14, 2008 <pre> <?php $fig = array ( '1,249', '33,182', '33182', '9,333', '3,981,847', '9,149', '3,294', '17,928', '1,413', '2,994' ); $fig = array_map(create_function('$num', 'return preg_replace("/\D/", "", $num);'), $fig); sort($fig); $fig = array_map(create_function('$num', 'return number_format($num);'), $fig); print_r($fig); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/128410-solved-having-issues-sorting-numbers-with-sort/#findComment-665331 Share on other sites More sharing options...
DamienRoche Posted October 14, 2008 Author Share Posted October 14, 2008 Thanks! Works perfectly and you've introduced me to a few new functions. Link to comment https://forums.phpfreaks.com/topic/128410-solved-having-issues-sorting-numbers-with-sort/#findComment-665333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.