Zesstra | b6ac9f6 | 2020-01-21 11:11:16 +0100 | [diff] [blame] | 1 | SYNOPSIS |
| 2 | mixed * sort_array(mixed *arr, string wrong_order) |
| 3 | mixed * sort_array(mixed *arr, string wrong_order, object|string ob) |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 4 | mixed * sort_array(mixed *arr, string wrong_order, object|string ob |
| 5 | , mixed extra...) |
Zesstra | b6ac9f6 | 2020-01-21 11:11:16 +0100 | [diff] [blame] | 6 | mixed * sort_array(mixed *arr, closure cl) |
| 7 | mixed * sort_array(mixed *arr, closure cl, mixed extra...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 8 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 9 | DESCRIPTION |
| 10 | Sort the copy either by the ordering function ob->wrong_order(a, b), |
| 11 | or by the closure expression <cl>. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 13 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 16 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 17 | If the <arr> argument equals 0, the result is also 0. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 18 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 19 | <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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 22 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 23 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 26 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 27 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 30 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 31 | EXAMPLES |
| 32 | To sort an array |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 33 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 34 | arr = ({ 3, 8, 1, 3 }) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 35 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 36 | in ascending order with the help of the ordering function |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 37 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 38 | int is_greater (int a, int b) { |
| 39 | return a > b; |
| 40 | } |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 41 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 42 | the following uses of sort_array() are equivalent: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 43 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 44 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 49 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 50 | If no implicit shallow copy of <arr> should be made, pass <arr> as |
| 51 | reference: |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 52 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 53 | sort_array(&arr, #'>) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 54 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 55 | A more complicated example is to sort the array |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 56 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 57 | arr = ({ ({ "foo", 3 }), ({ "quux", 1 }), ... }) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 58 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 59 | in ascending order by the second element of each subarray. |
| 60 | For this, the ordering function should be like |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 61 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 62 | int is_greater (mixed *a, mixed *b) { |
| 63 | return a[1] > b[1]; |
| 64 | } |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 65 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 66 | HISTORY |
| 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 | |
| 70 | SEE ALSO |
Zesstra | b6ac9f6 | 2020-01-21 11:11:16 +0100 | [diff] [blame] | 71 | transpose_array(E), filter(E), map(E), alists(LPC) |