Note that there are some explanatory texts on larger screens.

plurals
  1. POSort using utf characters in mysql or php ? best solutions
    primarykey
    data
    text
    <p>Using MySQL, i'm selecting a list of songs in spanish that i would like to sort. Here's a list of names that is returned by the query:</p> <ul> <li>¡Decirevilla!</li> <li>Alhambra</li> <li>123 pasitos</li> <li>África</li> <li>Arroz</li> <li>Decir</li> </ul> <p>The sorted list should look like this:</p> <ul> <li>123 pasitos</li> <li>África</li> <li>Alhambra</li> <li>Arroz</li> <li>¡Decirevilla!</li> <li>Decir</li> </ul> <p>After all of the research i've read, i've concluded that there is no reasonable way to achieve this using MySQL. I've tried collation, charset, etc... but there is no way the character ¡, ?, etc... can by sorted accordingly to my desired result. Even the Á is not sorted the way i want to...</p> <p>Question 1: Is this a reasonable conclusion?</p> <p>I believe the only way to achieve this is by passing the results to an array in php and then sort the array using a custom function... all this using the function usort (need to sort by value and i don't care about maintaning the key association). Something similar to this:</p> <pre><code>function normalize($a, $b) { if ($a == $b) { return 0; } return ($a &lt; $b) ? -1 : 1; } $tracks = array(); while ($row = $result-&gt;fetch_assoc()) { $tracks[] = $row; } usort($tracks, 'normalize'); </code></pre> <p>Question 2: Is this the best way to achieve a custom sorting?</p> <p>Here's where i'm hitting a wall:</p> <p>Question 3: I have no idea how to create the normalize function to sort the names accordingly to my needs. How do i ignore certain characters (¡, ?, ', !, ¿) and how do i replace other characters with the natural equivalent (Á -> A, É -> E, etc..) I believe that by ignoring certain characters and replacing others, i can achieve the sorting i'm loojing for...</p> <p>Question 4: All this make sense? Am i on the right path?</p> <p>Thanks in advance for all your advice. Marco</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload