blob: 65c861e53706d4f46be0eadec7fc72517e2774de [file] [log] [blame]
Zesstrab6ac9f62020-01-21 11:11:16 +01001SYNOPSIS
2 mixed * sort_array(mixed *arr, string wrong_order)
3 mixed * sort_array(mixed *arr, string wrong_order, object|string ob)
Zesstra715ec202025-07-09 22:18:31 +02004 mixed * sort_array(mixed *arr, string wrong_order, object|string ob
5 , mixed extra...)
Zesstrab6ac9f62020-01-21 11:11:16 +01006 mixed * sort_array(mixed *arr, closure cl)
7 mixed * sort_array(mixed *arr, closure cl, mixed extra...)
MG Mud User88f12472016-06-24 23:31:02 +02008
Zesstra715ec202025-07-09 22:18:31 +02009DESCRIPTION
10 Sort the copy either by the ordering function ob->wrong_order(a, b),
11 or by the closure expression <cl>.
MG Mud User88f12472016-06-24 23:31:02 +020012
Zesstra715ec202025-07-09 22:18:31 +020013 Usually a shallow copy of <arr> is made first and the sorted copy is
14 returned as result. However, if <arr> is given as a reference, no copy
15 will be made and <arr> will be sorted in-place.
MG Mud User88f12472016-06-24 23:31:02 +020016
Zesstra715ec202025-07-09 22:18:31 +020017 If the <arr> argument equals 0, the result is also 0.
MG Mud User88f12472016-06-24 23:31:02 +020018
Zesstra715ec202025-07-09 22:18:31 +020019 <ob> is the object in which the ordering function is called
20 and may be given as object or by its filename. If <ob> is omitted
21 or neither a string nor an object, it defaults to this_object().
MG Mud User88f12472016-06-24 23:31:02 +020022
Zesstra715ec202025-07-09 22:18:31 +020023 The elements from the array to be sorted are passed in pairs to
24 the function <wrong_order> as arguments, followed by the <extra>
25 arguments if any.
MG Mud User88f12472016-06-24 23:31:02 +020026
Zesstra715ec202025-07-09 22:18:31 +020027 The function should return a positive number if the elements
28 are in the wrong order. It should return 0 or a negative
29 number if the elements are in the correct order.
MG Mud User88f12472016-06-24 23:31:02 +020030
Zesstra715ec202025-07-09 22:18:31 +020031EXAMPLES
32 To sort an array
MG Mud User88f12472016-06-24 23:31:02 +020033
Zesstra715ec202025-07-09 22:18:31 +020034 arr = ({ 3, 8, 1, 3 })
MG Mud User88f12472016-06-24 23:31:02 +020035
Zesstra715ec202025-07-09 22:18:31 +020036 in ascending order with the help of the ordering function
MG Mud User88f12472016-06-24 23:31:02 +020037
Zesstra715ec202025-07-09 22:18:31 +020038 int is_greater (int a, int b) {
39 return a > b;
40 }
MG Mud User88f12472016-06-24 23:31:02 +020041
Zesstra715ec202025-07-09 22:18:31 +020042 the following uses of sort_array() are equivalent:
MG Mud User88f12472016-06-24 23:31:02 +020043
Zesstra715ec202025-07-09 22:18:31 +020044 arr = sort_array(arr, "is_greater", this_object())
45 arr = sort_array(arr, "is_greater")
46 arr = sort_array(arr, #'is_greater)
47 arr = sort_array(arr, #'>) (this is the preferred variant :-)
48 arr = sort_array(arr, lambda(({'a, 'b}), ({#'>, 'a, 'b})))
MG Mud User88f12472016-06-24 23:31:02 +020049
Zesstra715ec202025-07-09 22:18:31 +020050 If no implicit shallow copy of <arr> should be made, pass <arr> as
51 reference:
MG Mud User88f12472016-06-24 23:31:02 +020052
Zesstra715ec202025-07-09 22:18:31 +020053 sort_array(&arr, #'>)
MG Mud User88f12472016-06-24 23:31:02 +020054
Zesstra715ec202025-07-09 22:18:31 +020055 A more complicated example is to sort the array
MG Mud User88f12472016-06-24 23:31:02 +020056
Zesstra715ec202025-07-09 22:18:31 +020057 arr = ({ ({ "foo", 3 }), ({ "quux", 1 }), ... })
MG Mud User88f12472016-06-24 23:31:02 +020058
Zesstra715ec202025-07-09 22:18:31 +020059 in ascending order by the second element of each subarray.
60 For this, the ordering function should be like
MG Mud User88f12472016-06-24 23:31:02 +020061
Zesstra715ec202025-07-09 22:18:31 +020062 int is_greater (mixed *a, mixed *b) {
63 return a[1] > b[1];
64 }
MG Mud User88f12472016-06-24 23:31:02 +020065
Zesstra715ec202025-07-09 22:18:31 +020066HISTORY
67 LDMud 3.2.8 added the support of extra arguments.
68 LDMud 3.3.720 added the support of references to sort in-place.
69
70SEE ALSO
Zesstrab6ac9f62020-01-21 11:11:16 +010071 transpose_array(E), filter(E), map(E), alists(LPC)