Page 1 of 1
reverse natcasesort( )???? Any ideas on how to implement??
Posted: Wed Jun 28, 2006 6:42 am
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.
Posted: Wed Jun 28, 2006 8:02 am
by feyd
what about using
array_reverse()?
Posted: Wed Jun 28, 2006 8:03 am
by aerodromoi
Posted: Wed Jun 28, 2006 6:07 pm
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!)
Posted: Thu Jun 29, 2006 1:23 am
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";
}
?>
Posted: Thu Jun 29, 2006 2:32 am
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";
}
?>
Posted: Thu Jun 29, 2006 2:35 am
by Weirdan
or even:
Code: Select all
function reversenat($str_a, $str_b)
{
return -strnatcasecmp($str_a, $str_b);
}
Posted: Thu Jun 29, 2006 3:42 am
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...