Sorting Arrays

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Sorting Arrays

Postby Xerpher » Wed Oct 09, 2002 7:00 am

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?

Jay

Postby Jay » Wed Oct 09, 2002 2:12 pm

Doesn't sort() work?

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 09, 2002 2:18 pm

My previous example was using sort()

Jay

Postby Jay » Wed Oct 09, 2002 2:20 pm

Try using different flag strings for the second argument

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Wed Oct 09, 2002 6:02 pm

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.

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 :) iot only matters that they are correctly in alphabetical order.

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Thu Oct 10, 2002 7:39 am

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:

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.

Jay

Postby Jay » Thu Oct 10, 2002 2:48 pm

Why don't you use the natcasesort() function, and then use a for loop to reset the keys afterwards

Code: Select all

$sorted = natcasesort($myArray);
while(list(,$val) = each($sorted)) $newArray[] = $val;

Wouldn't that do what you want?

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Thu Oct 10, 2002 5:24 pm

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);


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron