Difference between revisions of "Tutorial:Advanced List Sorting"

From GeoGebra Manual
Jump to: navigation, search
(improve for list of NAMES)
(use Florian's command to sort names)
Line 24: Line 24:
 
==And for the names ? ==
 
==And for the names ? ==
 
The previous approach can seem frustrating, if we want to get back the orderly list of the '''names''' of the objects.<br/>
 
The previous approach can seem frustrating, if we want to get back the orderly list of the '''names''' of the objects.<br/>
Define at first, the list of names (as texts) <code>listD={"name1","name2", ... }</code>.
+
Define at first, the list of names (as texts) <code>list_n={"name1","name2", ... }</code>.
  
The <code>list</code>  of the algorithm above, will then be defined by <code>list=Sequence[Object[Element[listD, k]], k, 1, Length[listD]]</code>.
+
The <code>list</code>  of the algorithm above, will then be defined by <code>list=Sequence[Object[Element[list_n, k]], k, 1, Length[list_n]]</code>.
  
Once, your list sorted out in  <code>list_s </code>, you analyze the ranks of each of the objects, by the command:
 
<code>RangNames=RemoveUndefined[Join[Sequence[Sequence[If[Element[list_s, i] ≟ Element[list, j], i], i, 1, Length[list]], j, 1, Length[list]]]]</code>
 
  
 
And now, you can obtain the orderly list of names by the command :
 
And now, you can obtain the orderly list of names by the command :
  
<code>listA=Sequence[Element[listD, IndexOf[j, RangNames]], j, 1, Length[listD]]</code>
+
<code>list_{sn}=Sequence[Element[list_n, y(Element[list_h, k])], k, 1, Length[list_n]]</code>
 +
 
  
  

Revision as of 22:38, 12 April 2012

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

  1. Let list be the list we want to sort.
  2. list_h = Sort[Sequence[(*number based upon Element[list, i]*, i), i, 1, Length[list]]]
    This corresponds to c in the explanation (the helper list). The bold section is called f in the explanation.
  3. 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 (a_1, a_2, \ldots, a_n) \subset X and a mapping f: X \to \mathbb R we want to find a permutation \sigma \in S_n such that f(a_{\sigma(k)}) \leq f(a_{\sigma(l)}) \; \forall (k,l) \in \mathbb N^2: 1 \leq k \leq l \leq n. You can think of the permutation \sigma as a renumbering such that (a_{\sigma(1)}, a_{\sigma(2)}, \ldots, a_{\sigma(n)}) will be the sorted list.

So we first have to come up with a mapping f for the correct sorting, for example:

Example: If want to sort a list of points such that their distance to the origin is increasing we have X := \mathbb R^2 and f((x,y)) := \sqrt{x^2 + y^2}. In GeoGebra f for a point A would be Length[A], so the bold section in the algorithm would read Length[Element[list,i]].
Example: If 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 \tilde c = (\tilde c_1, \ldots, \tilde c_n) with \tilde c_k := (f(a_k), k) and sort this list. Let's call the sorted list c = (c_1, \ldots, c_n). This sorted list now allows us to construct \sigma, because y(c_{\sigma(k)}) = k. Using \sigma we can then construct the sorted version of our original list.

And for the names ?

The previous approach can seem frustrating, if we want to get back the orderly list of the names of the objects.
Define at first, the list of names (as texts) list_n={"name1","name2", ... }.

The list of the algorithm above, will then be defined by list=Sequence[Object[Element[list_n, k]], k, 1, Length[list_n]].


And now, you can obtain the orderly list of names by the command :

list_{sn}=Sequence[Element[list_n, y(Element[list_h, k])], k, 1, Length[list_n]] es:Tutorial:Orden de Listas Avanzado fr:Tutoriel:Tri pointu pour une liste

© 2021 International GeoGebra Institute