This article explains how the online converter "Sort Lists" works. With this tool you can sort lists according to different criteria easily.
Table of contents
Different sorting methods
A list can be sorted with different methods. This online tool supports alphabetic sorting according to DIN 5007-1, phonebook sorting according to DIN 5007-2, natural sorting, as well as the reverse sort order. It is possible to arrange the sorted list in ascending order or in descending order and you can also determine if the sorting should be case sensitive or not.
Alphabetical Sorting (DIN 5007-1)
It should be clear, how to sort a list alphabetically. Only for a few characters, such as the german umlauts Ä, Ö and Ü, it is not immediately obvious how these should be classified. According to DIN 5007-1, this is done as follows:
- ä/Ä and a/A are equal
- ö/Ö and o/O are equal
- ü/Ü and u/U are equal
- ß and ss are equal
You can program this in PHP the following way:
$sorted_list = array(); $search = array("#Ä#u", "#ä#u", "#Ö#u", "#ö#u", "#Ü#u", "#ü#u", "#ß#u", "#-#u"); $replace = array("A", "a", "O", "o", "U", "u", "ss", " "); $arr_replaced = preg_replace($search, $replace, $all_entries); // Replace characters // If it shall be sorted case sensitive if ($case_sensitive) { asort($arr_replaced, SORT_STRING); // Sort alphabetically } // If it shall not be sorted case sensitive else { asort($arr_replaced, SORT_STRING | SORT_FLAG_CASE); // Sort alphabetically } foreach ($arr_replaced as $key => $value) $sorted_list[] = $all_entries[$key];
In principle, an additional array is created, in which the umlauts etc. are replaced. Afterwards this is sorted and then the original array is arranged in the same order.
Phonebook Sorting (DIN 5007-2)
In the so-called phonebook sorting according to DIN 5007-2, the umlauts are replaced in another way:
- ä/Ä and ae/Ae are equal
- ö/Ö and oe/Oe are equal
- ü/Ü and ue/Ue are equal
- ß and ss are equal
Furthermore, not the normal alphabetical sorting but the natural sorting is applied. In natural sorting, entries with numerical values are sorted in the same way a human would do. For a better understanding, have a look at this example:
- Image1.png
- Image13.png
- Image2.png
- Image20.png
- Image1.png
- Image2.png
- Image13.png
- Image20.png
This is the code for the phonebook sorting:
$sorted_list = array(); $search = array("#Ä#u", "#ä#u", "#Ö#u", "#ö#u", "#Ü#u", "#ü#u", "#ß#u", "#-#u"); $replace = array("Ae", "ae", "Oe", "oe", "Ue", "ue", "ss", " "); $arr_replaced = preg_replace($search, $replace, $all_entries); // Replace characters // If it shall be sorted case sensitive if ($case_sensitive) { natsort($arr_replaced); // Replace characters } // If it shall not be sorted case sensitive else { natcasesort($arr_replaced); // Replace characters } foreach ($arr_replaced as $key => $value) { $sorted_list[] = $all_entries[$key]; }
Natural Sorting
The natural sorting was already explained in the previous section. The difference between this sorting method and the phonebook sorting is, that the umlauts etc. are replaced in the same way as in the alphabetical sorting:
$sorted_list = array(); $search = array("#Ä#u", "#ä#u", "#Ö#u", "#ö#u", "#Ü#u", "#ü#u", "#ß#u", "#-#u"); $replace = array("A", "a", "O", "o", "U", "u", "ss", " "); $arr_replaced = preg_replace($search, $replace, $all_entries); // Replace characters // If it shall be sorted case sensitive if ($case_sensitive) { natsort($arr_replaced); // Replace characters } // If it shall not be sorted case sensitive else { natcasesort($arr_replaced); // Replace characters } foreach ($arr_replaced as $key => $value) { $sorted_list[] = $all_entries[$key]; }
Reverse Sorting
For the reverse sorting, we just have to reverse the array with the entries:
$sorted_list = array_reverse($all_entries);
The same function is also used when selecting a sorting method in decending order.
TO THE LIST SORTER