I was just wondering if anyone knew of a simple way to sort my Array in alphabetical order... CORRECTLY!
Right now when I sort my array it works fine except it puts words starting with a lowercase letter after all the words starting with an uppercase letter.
Ex:
[1] => "Abc"
[2] => "Word"
[3] => "lowercase"
[4] => "Uppercase"
will sort into this:
[1] => "Abc"
[2] => "Uppercase"
[3] => "Word"
[4] => "lowercase"
So is there a quick fix or will I need to make my own sorting function to prevent this?
Sorting Arrays
Moderators: egami, macek, gesf
-
- New php-forum User
- Posts: 164
- Joined: Tue Aug 27, 2002 8:25 pm
- Location: Ontario, Canada
- Contact:
Nope that didn't work, but its ok now because I ended up making a small chunk of code to fix my problem because I tried all the sort functions I could find on php.net with no success.
Now on the one hand it does make all the values lowercase, but for what I'm doing it doesn't matter
iot only matters that they are correctly in alphabetical order.
Code: Select all
foreach($Array as $key => $value)
{
$Array[$key] = strtolower($value);
}
sort($Array);
Now on the one hand it does make all the values lowercase, but for what I'm doing it doesn't matter

-
- New php-forum User
- Posts: 164
- Joined: Tue Aug 27, 2002 8:25 pm
- Location: Ontario, Canada
- Contact:
I know that, but although natcasesort() does sort it the way I need it to, it doesn't change the array's keys as well as the values. I need it to sort it and then keep it in that order inside the array... all the natsorts only do this:
So yes its in the right order, but the keys still keep the array unsorted.
Code: Select all
Standard sorting
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
Natural order sorting
Array
(
[0] => img1.png
[3] => img2.png
[1] => img10.png
[2] => img12.png
)
So yes its in the right order, but the keys still keep the array unsorted.
Why don't you use the natcasesort() function, and then use a for loop to reset the keys afterwards
Wouldn't that do what you want?
Code: Select all
$sorted = natcasesort($myArray);
while(list(,$val) = each($sorted)) $newArray[] = $val;
Wouldn't that do what you want?
-
- New php-forum User
- Posts: 164
- Joined: Tue Aug 27, 2002 8:25 pm
- Location: Ontario, Canada
- Contact:
I guess I could... Either that two lines of code, or this two lines of code... Doesn't really matter, they both give the same end result.
Code: Select all
foreach($LinkListTitle as $key => $value) {$LinkListTitle[$key] = strtolower($value);}
sort($LinkListTitle);