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?? 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) 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 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. 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 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(). Link to comment https://forums.phpfreaks.com/topic/228861-sorting-numbers-numerically/#findComment-1179827 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.