Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Reverse Natural Sort? Eg: 576,148,3,1
Old 09-25-2010, 11:29 PM Reverse Natural Sort? Eg: 576,148,3,1
Skilled Talker

Posts: 56
Trades: 0
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
pcmaster160 is offline
Reply With Quote
View Public Profile Visit pcmaster160's homepage!
 
 
Register now for full access!
Old 09-25-2010, 11:37 PM Re: Reverse Natural Sort? Eg: 576,148,3,1
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
rsort
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-25-2010, 11:39 PM Re: Reverse Natural Sort? Eg: 576,148,3,1
Skilled Talker

Posts: 56
Trades: 0
Quote:
Originally Posted by NullPointer View Post
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
pcmaster160 is offline
Reply With Quote
View Public Profile Visit pcmaster160's homepage!
 
Old 09-25-2010, 11:43 PM Re: Reverse Natural Sort? Eg: 576,148,3,1
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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:
Code:
1,4,2,9,6,3,5
you should get back
Code:
9,6,5,4,3,2,1
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-25-2010 at 11:49 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-26-2010, 12:29 AM Re: Reverse Natural Sort? Eg: 576,148,3,1
Skilled Talker

Posts: 56
Trades: 0
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
pcmaster160 is offline
Reply With Quote
View Public Profile Visit pcmaster160's homepage!
 
Old 09-26-2010, 01:00 AM Re: Reverse Natural Sort? Eg: 576,148,3,1
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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
)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-26-2010, 02:18 PM Re: Reverse Natural Sort? Eg: 576,148,3,1
Skilled Talker

Posts: 56
Trades: 0
Quote:
Originally Posted by NullPointer View Post
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
pcmaster160 is offline
Reply With Quote
View Public Profile Visit pcmaster160's homepage!
 
Reply     « Reply to Reverse Natural Sort? Eg: 576,148,3,1
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34703 seconds with 12 queries