# Tutorial:Advanced List Sorting

If you want to sort a list of objects in GeoGebra it is not unlikely that the kind of sorting you want is not possible with the Sort command. (If you just want to inverse the results you can use `Sort[]`

together with Reverse[]).

But in case the kind of object is not supported by `Sort[]`

or you want a completely different sorting criterion this tutorial may help you with your task. If you're having problem with the algorithm you can read the explanation and examples below.

## Algorithm

- Let
`list`

be the list we want to sort. -
`list_h = Sort[Sequence[(`

***number based upon Element[list, i]***, i), i, 1, Length[list]]]

This corresponds to in the explanation (the helper list). The bold section is called in the explanation. -
`list_s = Sequence[Element[list, y(Element[list_h, i])], i, 1, Length[list]]`

This is the sorted list.

## Explanation

To clarify the situation let us try to formulate the task mathematically: Given a list

and a mapping we want to find a permutation such that . You can think of the permutation as a renumbering such that will be the sorted list.So we first have to come up with a mapping

for the correct sorting, for example:**Example:**If you want to sort a list of points such that their distance to the origin is increasing we have and . In GeoGebra for a point

`A`

would be `Length[A]`

, so the bold section in the algorithm would read `Length[Element[list,i]]`

.**Example:**If you want to sort a list of circles such that their radius is increasing the bold section in the algorithm would read

`Radius[Element[list,i]]`

.**Example:**A complete construction to sort a list of complex numbers by their argument can be found on GeoGebra: http://www.geogebra.org/material/show/id/7518

The trick to solve this problem in GeoGebra is to use the existing command to sort a list of points. Instead of thinking of points geometrically we think of those just as pairs of numbers. GeoGebra allows us to sort those points by their x-coordinates, so we create a list of points

with and sort this list. Let's call the sorted list . This sorted list now allows us to construct , because . Using we can then construct the sorted version of our original list.## And for the names?

Imagine we have constructed five points called `A`

, `B`

, `C`

, `D`

and `E`

and we want to sort those points based upon some criterion. Using `list = {A,B,C,D,E}`

the algorithm above can be used to achieve this, but it will just return a sorted list with the coordinates of the points, not a sorted list with their original names, which could have a meaning in the construction and thus also helpful to display to the user. To also display those names in a sorted manner we have to extend our algorithm slightly:

- Let
`list_n`

be a list with the*names of the objects*we want to sort.

For example:`list_n = {"A", "B","C","D","E"}`

. Do not forget the quotes: We want the names of the objects and not the objects themselves! - Define
`list=Sequence[Object[Element[list_n, k]], k, 1, Length[list_n]]`

.

This creates the list of objects we want to sort based upon the name list. -
`list_h = Sort[Sequence[(`

***number based upon Element[list, i]***, i), i, 1, Length[list]]]

This is step 2 of our original algorithm. -
`list_{ns} = Sequence[Element[list_n, y(Element[list_h, i])], i, 1, Length[list]]`

This is now a list with the sorted names of the objects, like`{"B", "A", "E", "F", "D"}`

. If you also want a sorted list with the values you can just use`list_s`

as defined in the original algorithm.

**Example:**The third example from above using this modified algorithm to also display object names can be found here: http://www.geogebra.org/material/show/id/7775

fr:Tutoriel:Tri pointu pour une liste de:Anleitungen:Listen von beliebigen Elementen sortieren it:Tutorial:Ordinare gli elementi di una lista