This site works best with JavaScript enabled!

Description: Sort Lists

Online tool: Sort list

This article explains how the online converter "Sort Lists" works. With this tool you can sort lists according to different criteria easily.

Online tool "Sort Lists"

Click here to go to the converter described below.

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:

1
2
3
4
5
6
7
8
9
10
11
12
$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:

Alphabetical Sorting
  • Image1.png
  • Image13.png
  • Image2.png
  • Image20.png
Natural Sorting
  • Image1.png
  • Image2.png
  • Image13.png
  • Image20.png

This is the code for the phonebook sorting:

1
2
3
4
5
6
7
8
9
10
11
12
$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:

1
2
3
4
5
6
7
8
9
10
11
12
$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:

1
$sorted_list = array_reverse($all_entries);

The same function is also used when selecting a sorting method in decending order.

Share this page
Posted on 04.03.2018 | Last modified on 24.05.2018