reverse natcasesort( )???? Any ideas on how to implement??
Moderator: General Moderators
reverse natcasesort( )???? Any ideas on how to implement??
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.
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
what about using array_reverse()?
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!)
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!)
- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
Just to give you an idea: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!)
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";
}
?>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";
}
?>or even:
Code: Select all
function reversenat($str_a, $str_b)
{
return -strnatcasecmp($str_a, $str_b);
}- aerodromoi
- Forum Contributor
- Posts: 230
- Joined: Sun May 07, 2006 5:21 am
And I'm running a switch statement...Weirdan wrote:or even:Code: Select all
function reversenat($str_a, $str_b) { return -strnatcasecmp($str_a, $str_b); }