Scummy12 Posted February 26, 2011 Share Posted February 26, 2011 I have a txt file containing many numbers. I need to sort the values numerically from highest to lowest. When i use the rsort function, it sorts the numbers first by how many numbers long it is. E.g. 1,000 is sorted higher than 10,000. Any idea how I can fix this?? Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/ Share on other sites More sharing options...
ChemicalBliss Posted February 26, 2011 Share Posted February 26, 2011 sort($number_array, SORT_NUMERIC) www.php.net/sort (The same can be applied to rsort,asort,krsort and ksort etc) Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179811 Share on other sites More sharing options...
Scummy12 Posted February 26, 2011 Author Share Posted February 26, 2011 The same error occurs, that is what I have been using. But it sorts the numbers by how many characters in the number string before it sorts it numerically Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179817 Share on other sites More sharing options...
PFMaBiSmAd Posted February 26, 2011 Share Posted February 26, 2011 Do your numbers have commas in them or was the example in your first post just an example? A comma as a thousand's separator is a human-readable aid and computers treat the comma as a stop character. Posting an actual example of your data and the code you tried would be the quickest way of getting a solution. Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179821 Share on other sites More sharing options...
Scummy12 Posted February 26, 2011 Author Share Posted February 26, 2011 Thank you very much, I did have commas and removing them solved my problem. Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179824 Share on other sites More sharing options...
jcbones Posted February 26, 2011 Share Posted February 26, 2011 You can add the comma's back in using number_format(). Quote Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179827 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.