framework icon indicating copy to clipboard operation
framework copied to clipboard

Collection sort method don't sort lists with accented characters

Open allanmcarvalho opened this issue 2 years ago • 0 comments

  • Laravel Version: 9.23.0
  • PHP Version: 8.1.1
  • Database Driver & Version: MySQL 5.7.34

Description:

In brazilian portuguese language (and in others too), we have a lot of words with accentuation and sometime we need to collect a list that contain that kind of word. With sort() method is impossible to sort especially if the word starts with an accentuation. I know that it is possible to resolve this using a callback like this in sort method:

function($a, $b) {
    $at = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
    $bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
    return strcmp($at, $bt);
})

But I really thing that is a big internationalization improvement at Collection class.

Steps To Reproduce:


$collection = collect(["Administrativo", "Álbuns", "Outros", "Especial"])->sort();

dd($collection->toArray());
// Will produce
//   [0 => "Administrativo", 3 => "Especial",  2 => "Outros",  1 => "Álbuns"]
// Instead
//   [0 => "Administrativo", 1 => "Álbuns", 3 => "Especial",  2 => "Outros"]

Using the closure above works like a charm!

allanmcarvalho avatar Aug 10 '22 20:08 allanmcarvalho

I don't think we can make any real big changes to a method that has worked the same over all these years, sorry.

driesvints avatar Aug 11 '22 06:08 driesvints