Sorting string-array without case...

Hi!

I user sort() but it gives me all uppercase items first.
Looking at http://ee.php.net/manual/en/function.sort.php I cannot find
what I need - tried natcasesort, but that was even more
interesting....

sort gives me (yes, I am aware of the spelling error)

Doc manager
Doc manger more
Tango
brugs

while natcasesort gives me

Doc manager
Tango
Doc manger more
brugs

and I want

brugs
Doc manager
Doc manger more
Tango

just as my alphabet is :-)
What should I user...
jodleren [ Mi, 16 Januar 2008 16:40 ] [ ID #1909723 ]

Re: Sorting string-array without case...

On Jan 16, 10:40 am, jodleren <sonn... [at] hot.ee> wrote:
> Hi!
>
> I user sort() but it gives me all uppercase items first.
> Looking athttp://ee.php.net/manual/en/function.sort.phpI cannot find
> what I need - tried natcasesort, but that was even more
> interesting....
>
> sort gives me (yes, I am aware of the spelling error)
>
> Doc manager
> Doc manger more
> Tango
> brugs
>
> while natcasesort gives me
>
> Doc manager
> Tango
> Doc manger more
> brugs
>
> and I want
>
> brugs
> Doc manager
> Doc manger more
> Tango
>
> just as my alphabet is :-)
> What should I user...

How about this (untested):

$arr = array('foo', 'bar', 'Baz');
usort($arr, create_function('$v1,$v2', 'return strcasecmp($v1,
$v2);'));
zeldorblat [ Mi, 16 Januar 2008 17:49 ] [ ID #1909733 ]

Re: Sorting string-array without case...

On Jan 16, 6:49=A0pm, ZeldorBlat <zeldorb... [at] gmail.com> wrote:
> On Jan 16, 10:40 am, jodleren <sonn... [at] hot.ee> wrote:
>
>
>
>
>
> > Hi!
>
> > I user sort() but it gives me all uppercase items first.
> > Looking athttp://ee.php.net/manual/en/function.sort.phpIcannot find
> > what I need - tried natcasesort, but that was even more
> > interesting....
>
> How about this (untested):
>
> $arr =3D array('foo', 'bar', 'Baz');
> usort($arr, create_function('$v1,$v2', 'return strcasecmp($v1,
> $v2);'));- Hide quoted text -
>
Yep, that does it. Thanks

I implemented it as this, which makes it work as a normal sort.

function my_sort(&$arr)
{
usort($arr, create_function('$v1,$v2', 'return
strcasecmp($v1,$v2);'));
}
jodleren [ Do, 17 Januar 2008 10:25 ] [ ID #1910717 ]
PHP » comp.lang.php » Sorting string-array without case...

Vorheriges Thema: [OT] Sun to acquire MySQL for $1B
Nächstes Thema: Internet Application Developer - Contract 6 months