1

im having troubles with sort functions.

When i try sort in array a got bad order results.. For example:

$arr = array("Cero","Uno","dos","Tres","Cuatro","Cinco","Seis","Siete");
sort($arr);
foreach($arr as $key => $value){
    echo $key . "=>" . $value . "<br/>";
}

Write :

0=>Cero
1=>Cinco
2=>Cuatro
3=>Seis
4=>Siete
5=>Tres
6=>Uno
7=>dos //wtf, last item are "d" ?!

If i try with rsort i got this:

0=>dos //again, last item are "d" ?!
1=>Uno
2=>Tres
3=>Siete
4=>Seis
5=>Cuatro
6=>Cinco
7=>Cero

I tried asort, arsort, sort and rsort, but always returns a bad order. You know why? or a method to fix it? Thanks!

Carasuman
  • 636
  • 1
  • 7
  • 17
  • "I tried asort, arsort, sort and rsort, but always returns a bad order." --- that happened because it's always useful to **read** documentation, not just apply the functions randomly – zerkms Oct 24 '12 at 23:22
  • Here is your answer: http://stackoverflow.com/questions/7763936/sort-array-items-in-php-so-that-it-is-not-case-sensitive-to-letters – Hernan Velasquez Oct 24 '12 at 23:22
  • @zerkms if u read here http://php.net/manual/en/array.sorting.php, u will see is not "just apply the functions randomly" if you read "sort by value" and "low to high/high to low" obviusly u think "this works ci and not ci". You have never missed a detail while developing a large project? – Carasuman Oct 24 '12 at 23:27
  • @Carasuman: this has nothing to do with a project size. It's about reading the documentation for the function you use: http://www.php.net/manual/en/function.sort.php – zerkms Oct 24 '12 at 23:28
  • @zerkms Thats a little more constructive comment. – Carasuman Oct 24 '12 at 23:30
  • @Carasuman: I thought it's obvious - to read documentation. So I couldn't even realize you asked a question before checking it. – zerkms Oct 24 '12 at 23:31

2 Answers2

6

you want to pass the flag to ignore case.

sort($arr, SORT_FLAG_CASE);
jarchuleta
  • 1,231
  • 8
  • 10
1

depending on your version of php you can use either, sort() or natcasesort()

since the release of php5.4, there is a flag called SORT_FLAG_CASE, which you can use with the sort function

sort($array, SORT_FLAG_CASE);

since not every sever runs php 5.4, you should at least know that before that, you used the natcasesort function

natcasesort($array);

for more information just check the php.net manual

Stefan
  • 718
  • 9
  • 13