Jump to content

[SOLVED] having issues sorting numbers with sort()


DamienRoche

Recommended Posts

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.

<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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.