scnadir sort order asc put uppercase letters first

247 views Asked by At

So I am trying to sort a list of folders and files and display them alphabetical the problem appears that if someone created a folder that starts with a captial letter that folder appears first for example if I had the following folders

Array
(
    [0] => .
    [1] => ..
    [2] => _base
    [3] => template
    [4] => Website
)

I would expect when using scandir (scandir($directory, SCANDIR_SORT_ASCENDING)) to see the folders listed out as above but instead that get listed out as

Array
(
    [0] => .
    [1] => ..
    [2] => Website
    [3] => _base
    [4] => template
)

How would I be able to get this list sorted the correct way so that it wouldn't be case sensitive.

2

There are 2 answers

0
Rizier123 On BEST ANSWER

This should work for you:

natcasesort($array);

It sort's an array natural ignore case

See: http://php.net/manual/en/function.natcasesort.php

0
Jazi On

Maybe You should just use sort() function?

Example:

$x = array('.', '..', '22331', 'djsnaso', 'Aijndod', 'Wwwwww');

sort($x);

var_dump($x);

Return value:

array (size=6)
  0 => string '.' (length=1)
  1 => string '..' (length=2)
  2 => string '22331' (length=5)
  3 => string 'Aijndod' (length=7)
  4 => string 'Wwwwww' (length=6)
  5 => string 'djsnaso' (length=7)