reverse natcasesort( )???? Any ideas on how to implement??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

reverse natcasesort( )???? Any ideas on how to implement??

Post by mcccy005 »

I want to use natcasesort( ). Fine. No worries.
But sometimes I want to use a reverse natcasesort.....from what I can find its not possible.
I don't understand usort (the function bit anyways) well enough to know if there is some way I can use this either???

Otherwise theres always the dodgy way of using natcasesort; and then swapping the last element to first; second last to second; third last to third etc etc.


Cheers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what about using array_reverse()?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

How about using usort?
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

like i said; i know nothing (very little) about writing a function to go into usort( ).
However, since there is such a thing as array_reverse( ); that makes life much simpler.
See, the sort is for columns on a table on a website; so the first time the user sorts the list, it will be sorted using natcasesort( ).
Now, if the user clicks it again, I can just call array_reverse( ); and then refresh the page....(well somehow - guess I'll learn how to do that next!)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

mcccy005 wrote:like i said; i know nothing (very little) about writing a function to go into usort( ).
However, since there is such a thing as array_reverse( ); that makes life much simpler.
See, the sort is for columns on a table on a website; so the first time the user sorts the list, it will be sorted using natcasesort( ).
Now, if the user clicks it again, I can just call array_reverse( ); and then refresh the page....(well somehow - guess I'll learn how to do that next!)
Just to give you an idea:

Code: Select all

<?php
$arr = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

function reversenat($str_a, $str_b)
{
   switch (strnatcasecmp($str_a,$str_b)){
     case 0:
       $ret = 0;
       break;
     case 1:
       $ret = -1;
       break;
     case -1:
       $ret = 1;
       break;
   } 
   return $ret;
}

usort($arr, "reversenat");

foreach ($arr as $key) {
   echo "$key: $value\n";
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

<?php
$arr = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

function reversenat($str_a, $str_b)
{
   $rets = array(-1 => 1, 0 => 0, 1 => -1);
   return $rets[strnatcasecmp($str_a, $str_b)];
}

usort($arr, "reversenat");

foreach ($arr as $key) {
   echo "$key: $value\n";
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or even:

Code: Select all

function reversenat($str_a, $str_b)
{
   return -strnatcasecmp($str_a, $str_b);
}
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Weirdan wrote:or even:

Code: Select all

function reversenat($str_a, $str_b)
{
   return -strnatcasecmp($str_a, $str_b);
}
And I'm running a switch statement...
Post Reply