Reverse Natural Sort? Eg: 576,148,3,1
09-25-2010, 11:29 PM
|
Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 56
|
I have a php script where i load pageloads into an array and i want a way to sort them from biggest to smallest. using normal reverse sort puts 9 above 500 and only does it by the first number. the only one that accurately sorts numbers is natsort or natural sort. but the problem is it ranks from smallest to greatest and i want from greatest to smallest. any way to do this? thanks guys.
__________________
Please login or register to view this content. Registration is FREE Free Online Games
|
|
|
|
09-25-2010, 11:37 PM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
|
|
|
|
09-25-2010, 11:39 PM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 56
|
Quote:
Originally Posted by NullPointer
|
thats what i meant by normal reverse sort. it only takes the first number so 9 would be before 500 ect...
__________________
Please login or register to view this content. Registration is FREE Free Online Games
|
|
|
|
09-25-2010, 11:43 PM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
I think I'm missing something. What do you mean by 'it only takes the first number'. Can you provide a sample input and the expected output? If you call rsort with input:
you should get back
PHP Code:
$foo = array(1,4,2,9,6,3,5);
rsort($foo);
print_r($foo);
will output
Code:
Array
(
[0] => 9
[1] => 6
[2] => 5
[3] => 4
[4] => 3
[5] => 2
[6] => 1
)
Edit:
I think I understand what's going on now. Its trying to sort the input as strings rather than integers. Lexicographically, 9 would be before 500 if you make the assumption they are strings, not numbers. Make sure you cast your input as integers.
Last edited by NullPointer; 09-25-2010 at 11:49 PM..
|
|
|
|
09-26-2010, 12:29 AM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 56
|
Thats weird i tried it in a simple script with nothing else and it worked. but when i try it with my array built with combining text files it doesn't work:
Before Sort:
Code:
Array ( [0] => 60 ~http://futuregamespc.com/demo/3d_racing.php~3d Racing~ [1] => 55 ~http://futuregamespc.com/demo/6_differences.php~6 Differences~ [2] => 284 ~http://futuregamespc.com/demo/countertest.php~Untitled 1~ [3] => 2 ~http://futuregamespc.com/demo/games.php~~ )
After Sort:
Code:
Array ( [0] => 60 ~http://futuregamespc.com/demo/3d_racing.php~3d Racing~ [1] => 55 ~http://futuregamespc.com/demo/6_differences.php~6 Differences~ [2] => 284 ~http://futuregamespc.com/demo/countertest.php~Untitled 1~ [3] => 2 ~http://futuregamespc.com/demo/games.php~~ )
php to sort it:
PHP Code:
$directory = "/mnt/w0210/d28/s25/b02a8bb2/www/counters/demo/";
//Directory is set;
$directory = (!strstr($directory,"*") || $directory =="./" ) ? $directory."*" : $directory;
//Checks if the wildcard operator is present, and if not it adds it by default at the end;
$files = glob($directory);
//Yes, it was that easy to get all the files;
$size=sizeof($files);
for($i=0;$i<sizeof($files) ; $i++){
//Loop through the files and adds to array;
$fp = fopen($files[$i],"r");
$contents[$i]=fgets($fp,999);
fclose($fp);
}
print_r ($contents); // print unsorted
rsort($contents);
print_r ($contents); //print sorted
$imploded = implode(" ", $contents); //get rid of spaces
$newcontent=explode("~", $imploded); // sort into chucks so i can display the data.
print_r ($newcontent);
__________________
Please login or register to view this content. Registration is FREE Free Online Games
|
|
|
|
09-26-2010, 01:00 AM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
The problem is that
Code:
60 ~http://futuregamespc.com/demo/3d_racing.php~3d Racing~
is a string (along with every other element in the array). It is a string that happens to begin with a number, but still a string.
What you need to do is use usort and write a custom comparison function that separates the integer from the rest of the string, then uses that to compare. Fortunately, because of the way casting works in PHP, all you have to do is cast each element to an int. For example:
PHP Code:
function cmp($a, $b)
{
$a = (int) $a;
$b = (int) $b;
if($a > $b)
return -1;
if($b > $a)
return 1;
return 0;
}
$foo = array('10 flskj', '3 kdslfj', '1 sfj', '93 dslkfj');
usort($foo, 'cmp');
print_r($foo);
will output:
Code:
Array
(
[0] => 93 dslkfj
[1] => 10 flskj
[2] => 3 kdslfj
[3] => 1 sfj
)
|
|
|
|
09-26-2010, 02:18 PM
|
Re: Reverse Natural Sort? Eg: 576,148,3,1
|
Posts: 56
|
Quote:
Originally Posted by NullPointer
The problem is that
Code:
60 ~http://futuregamespc.com/demo/3d_racing.php~3d Racing~
is a string (along with every other element in the array). It is a string that happens to begin with a number, but still a string.
|
Oh! Thank you so much! That makes complete sense. Didn't realize that php sorts them differently by what type of variable they are or even think of that possibility. Thank you so much! Its working now 
__________________
Please login or register to view this content. Registration is FREE Free Online Games
|
|
|
|
|
« Reply to Reverse Natural Sort? Eg: 576,148,3,1
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|