Jump to content

[SOLVED] sort() function not sorting numbers correctly


Someone789

Recommended Posts

Hi,

 

Basically, I'm using the rsort() function to sort a number of arrays from the largest number to the smallest number. However, it isn't working correctly.

 

I have a $numbers array, which has 25 arrays of numbers within it. Just to view what $numbers[1] looked like, I wrote this:

print_r($numbers[1]);

 

Yields:

Array ( [0] => 600072 [1] => 336099 [2] => 246016 [3] => 214041 [4] => 191549 [5] => 156316 [6] => 144808 [7] => 140885 [8] => 130908 [9] => 122126 [10] => 117487 [11] => 99993 [12] => 99763 [13] => 99041 [14] => 98775 [15] => 97630 [16] => 91849 [17] => 85420 [18] => 83651 [19] => 83329 [20] => 78742 [21] => 73963 [22] => 68829 [23] => 68082 [24] => 67398 [25] => 67198 [26] => 66813 [27] => 66319 [28] => 64493 [29] => 62050 [30] => 61954 [31] => 61576 [32] => 59285 [33] => 59120 [34] => 58611 [35] => 58052 [36] => 57963 [37] => 111 [38] => 55476 [39] => 54523 [40] => 51975 [41] => 50861 [42] => 49765 [43] => 222 [44] => 333 [45] => 47219 [46] => 47036 [47] => 46165 [48] => 44149 [49] => 44095 [50] => 56976 [51] => 47751 [52] => 47233 ) 

 

As you can see, the array is filled with some assorted numbers, from 111 to 600,072. I need the number sets from all 25 $number arrays to be ordered from largest to smallest, so I made this adjustment to my script and again kept in the print_r code to view what the new $numbers[1] array looked like:

for ($y = 0; $y <= 24; $y++) {

rsort($numbers[$y]);

}

print_r($numbers[1]);

 

Which yielded:

Array ( [0] => 99993 [1] => 99763 [2] => 99041 [3] => 98775 [4] => 97630 [5] => 91849 [6] => 85420 [7] => 83651 [8] => 83329 [9] => 78742 [10] => 73963 [11] => 68829 [12] => 68082 [13] => 67398 [14] => 67198 [15] => 66813 [16] => 66319 [17] => 64493 [18] => 62050 [19] => 61954 [20] => 61576 [21] => 600072 [22] => 59285 [23] => 59120 [24] => 58611 [25] => 58052 [26] => 57963 [27] => 56976 [28] => 55476 [29] => 54523 [30] => 51975 [31] => 50861 [32] => 49765 [33] => 47751 [34] => 47233 [35] => 47219 [36] => 47036 [37] => 46165 [38] => 44149 [39] => 44095 [40] => 336099 [41] => 333 [42] => 246016 [43] => 222 [44] => 214041 [45] => 191549 [46] => 156316 [47] => 144808 [48] => 140885 [49] => 130908 [50] => 122126 [51] => 117487 [52] => 111 )

 

However, as you can obviously see, this did not sort the numbers in descending order according to value, but instead by simply the first digit in the number. The largest number in the array - 600,072 - is in the middle with the rest of the 6s instead of out in front.

 

So, my question is: How can I sort these numbers so that their *values* are placed in descending order, and not just be ordered by their leading digit?

 

Many thanks in advance! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.