blob: 290cba2cf8ec1a93d95fb3024867c40614965e27 [file] [log] [blame]
Zesstra051ad6b2020-01-23 21:46:58 +01001sort_array
2**********
3
4
5BEMERKUNGEN
6===========
7
8 Achtung, die Elemente in 'arr' werden nicht tief kopiert, sind sie
9 also selbst Arrays oder Mappings, so fuehrt eine Aenderung im
10 Rueckgabe- Array zur Aenderung im Ursprungsarray.
11
12
13BEISPIELE
14=========
15
16 1. Sortieren von Zahlen in aufsteigender Reihenfolge
17
18 int *arr = ({ 3, 8, 1, 3 })
19
20 // Folgend identische Resultate, aber andere Ansaetze:
21 #1: nutzt die 'Efun' > als Lfun-Closure (ideal hier):
22 sort_array(arr, #'>);
23
24 #2: mit Sortierfunktion im selben Objekt:
25 int is_greater (int a, int b) {
26 return a > b;
27 }
28
29 #2a: sortiert mittels der Lfun im selben Objekt die Elemente in das
30 Rueckgabearray
31 sort_array(arr, "is_greater", this_object())
32 sort_array(arr, "is_greater")
33
34 #2b: nutzt die Lfun is_greater() als Lfun-Closure (Funktionspointer)
35 sort_array(arr, #'is_greater)
36
37 #3: Nutzt eine Inline-Closure
38 sort_array(arr, function int (int a, int b) {
39 return a > b; } );
40
41 Resultat in allen Faellen: ({1,3,3,8})
42
43 2. Sortieren von geschachtelten Arrays
44
45 arr = ({ ({ "foo", 3 }), ({ "quux", 1 }), ... })
46
47 // Vorgehen identisch, allerdings muss die Sortierfunktion
48 // angepasst werden:
49
50 int is_greater (<string|int> *a, <string|int> *b) {
51 return a[1] > b[1];
52 }