I’m creating a CSS chart that lists items from highest to lowest based on the number value. The problem is “rsort” seems to only count the first 5 digits (or so it seems). This is resulting in it showing items higher than 100,000 below the other numbers. Example of this problem is below:
$ITEM_1 = "95000";
$ITEM_2 = "103000";
..
$item_rank[]= "<li>$ITEM_1 Item 1</li>";
$item_rank[]= "<li>$ITEM_2 Item 2</li>";
..
rsort($item_rank); // sort highest numbers to lowest
echo "<ul>";
echo $item_rank[0];
echo $item_rank[1];
..
echo "</ul>";
In this case, Item 1 is actually a lower number, but it is being ranked higher because any item over 100,000 gets treated lower. Is there a way around this?
I believe you should be using natsort(). This happens when you try to sort numbers treated as string. Here’s an example:
But you are expecting an output like this:
To do that, use natsort and array_reverse():