...

View Full Version : sorting an array with natcasesort()



coffeedemon
11-02-2006, 06:48 PM
I have an array of directory names. sort() works fine but isn't case insensitive. So I am wanting to use natcasesort() but it's not sorting in any logical order that I can see. How can I sort this array case insensitive? >:(

This is the array BEFORE natcasesort():
Array
(
[manual] => Array
(
[name] => manual
[details] =>
)

[journal] => Array
(
[name] => journal
[details] =>
)

[stress_buster] => Array
(
[name] => stress_buster
[details] =>
)

[pocket_guide] => Array
(
[name] => pocket_guide
[details] =>
)

[Qcodes] => Array
(
[name] => Qcodes
[details] =>
)

[disc_wallet] => Array
(
[name] => disc_wallet
[details] =>
)

=> Array
(
[name] => bread_and_butter
[details] =>
)

[big_seminar] => Array
(
[name] => big_seminar
[details] =>
)

[uploads] => Array
(
[name] => uploads
[details] =>
)

[practitioner_training] => Array
(
[name] => practitioner_training
[details] =>
)

[success_codes] => Array
(
[name] => success_codes
[details] =>
)

[mp3] => Array
(
[name] => mp3
[details] =>
)

[truth_tech] => Array
(
[name] => truth_tech
[details] =>
)

[3_inch_sticker] => Array
(
[name] => 3_inch_sticker
[details] =>
)

[infomercial] => Array
(
[name] => infomercial
[details] =>
)

[testimonials_2006] => Array
(
[name] => testimonials_2006
[details] =>
)

[disc_label] => Array
(
[name] => disc_label
[details] =>
)

[binder] => Array
(
[name] => binder
[details] =>
)

)

[B]this is AFTER natcasesort(). Why is the order all messed up still?
Array
(
[truth_tech] => Array
(
[name] => truth_tech
[details] =>
)

[mp3] => Array
(
[name] => mp3
[details] =>
)

[success_codes] => Array
(
[name] => success_codes
[details] =>
)

[3_inch_sticker] => Array
(
[name] => 3_inch_sticker
[details] =>
)

[infomercial] => Array
(
[name] => infomercial
[details] =>
)

[binder] => Array
(
[name] => binder
[details] =>
)

[disc_label] => Array
(
[name] => disc_label
[details] =>
)

[testimonials_2006] => Array
(
[name] => testimonials_2006
[details] =>
)

[practitioner_training] => Array
(
[name] => practitioner_training
[details] =>
)

[uploads] => Array
(
[name] => uploads
[details] =>
)

[pocket_guide] => Array
(
[name] => pocket_guide
[details] =>
)

[stress_buster] => Array
(
[name] => stress_buster
[details] =>
)

[journal] => Array
(
[name] => journal
[details] =>
)

[Qcodes] => Array
(
[name] => Qcodes
[details] =>
)

[disc_wallet] => Array
(
[name] => disc_wallet
[details] =>
)

[big_seminar] => Array
(
[name] => big_seminar
[details] =>
)

[bread_and_butter] => Array
(
[name] => bread_and_butter
[details] =>
)

[manual] => Array
(
[name] => manual
[details] =>
)

)

ealbrecht
11-02-2006, 08:15 PM
http://ca3.php.net/manual/en/function.natcasesort.php


<?php
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natcasesort($array2);
echo "\nNatural order sorting (case-insensitive)\n";
print_r($array2);
?>


Standard sorting
Array
(
[0] => IMG0.png
[1] => IMG3.png
[2] => img1.png
[3] => img10.png
[4] => img12.png
[5] => img2.png
)

Natural order sorting (case-insensitive)
Array
(
[0] => IMG0.png
[4] => img1.png
[3] => img2.png
[5] => IMG3.png
[2] => img10.png
[1] => img12.png
)

coffeedemon
11-03-2006, 04:57 AM
Yes i've read that many times.

the second array i posted is the result i get with natcasesort(); which is not any kind of sorting.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum