|
| 1 | + |
| 2 | +# Sorting |
| 3 | + |
| 4 | +The need for being able to sort JSON output comes up repeatedly, and |
| 5 | +is a natural need. The question is how to work it into the language. |
| 6 | + |
| 7 | +## What can be sorted? |
| 8 | + |
| 9 | +Only arrays can be sorted. |
| 10 | + |
| 11 | +## Syntax |
| 12 | + |
| 13 | +Two obvious approaches present themselves immediately: a macro or an |
| 14 | +`order by` operator. |
| 15 | + |
| 16 | +### Custom syntax |
| 17 | + |
| 18 | +One could imagine adding an `order by` to list comprehensions: |
| 19 | + |
| 20 | +```[for (...) ... if (...) order by ... asc|desc]``` |
| 21 | + |
| 22 | +The same for object comprehensions doesn't make sense. |
| 23 | + |
| 24 | +It would also be possible to have `order by` as simply an operator: |
| 25 | + |
| 26 | +```.foo.bar order by .key``` |
| 27 | + |
| 28 | +Formally: `<expr> order by <expr>`, with priority to be established. |
| 29 | + |
| 30 | +### Macro |
| 31 | + |
| 32 | +A macro `sort` would work: |
| 33 | + |
| 34 | +```sort(<array to sort>, <expression to produce sort key>?)``` |
| 35 | + |
| 36 | +Thus: |
| 37 | + |
| 38 | +``` |
| 39 | +sort([5, 1, 3]) => [1, 3, 5] |
| 40 | +sort([5, 1, 3], -1 * .) => [5, 3, 1] |
| 41 | +``` |
| 42 | + |
| 43 | +One could also imagine a third parameter to choose between ascending |
| 44 | +and descending sort. However, a `reverse()` function might be useful |
| 45 | +for other purposes, too, so perhaps it would be better to add that, |
| 46 | +too. |
| 47 | + |
| 48 | +## Value ordering |
| 49 | + |
| 50 | +Numbers are easy: ordered according to natural numeric order. |
| 51 | + |
| 52 | +Strings are also easy: ordered according to Unicode code point. We |
| 53 | +make no attempt to support custom collations based on language or |
| 54 | +other preferences. Anyone wanting that will need to define functions |
| 55 | +to generate sort keys. |
| 56 | + |
| 57 | +Booleans: `false` sorts before `true`. |
| 58 | + |
| 59 | +Arrays: sorted according the nth element, beginning with the first. If |
| 60 | +the nth element of one array is smaller, then so is the array. If one |
| 61 | +array has no element in the nth position, that array is smaller. If |
| 62 | +the elements are equal then comparison moves to `n+1`. |
| 63 | + |
| 64 | +Objects? Sorting them is not really very nice. Python 3 does not allow |
| 65 | +sorting dicts at all, but Python 2 did. However, refusing to sort |
| 66 | +arrays with mixed types can easily lead to errors if the input is not |
| 67 | +shaped as expected, and we generally try to avoid that. So objects |
| 68 | +should be sortable. |
| 69 | + |
| 70 | +We don't really care about the order of objects, so for the sake of |
| 71 | +speed objects compare by size first. Then by the smallest key. Then by |
| 72 | +the value of the smallest key. If the smallest keys are the same |
| 73 | +comparison moves to the next key. |
| 74 | + |
| 75 | +The order of the types is `null`, booleans, numbers, strings, arrays, |
| 76 | +objects. |
| 77 | + |
| 78 | +# Use case solutions |
| 79 | + |
| 80 | +## Original motivation |
| 81 | + |
| 82 | +From [the original issue](https://github.com/schibsted/jslt/issues/172#issue-781566400) we have this example: "e.g. you can sort by |
| 83 | +the score field in the following example:" |
| 84 | + |
| 85 | +```[ { "id": "a", "score": 4}, { "id": "b", "score": 3}]``` |
| 86 | + |
| 87 | +There is some missing context here, but with an operator this would be: |
| 88 | + |
| 89 | +```. order by .score``` |
| 90 | + |
| 91 | +With a macro it would be: |
| 92 | + |
| 93 | +```sort(., .score)``` |
| 94 | + |
| 95 | +Github user hemakumarg89 also wanted to sort "json objects in an |
| 96 | +array(based on a field value)". That's basically the same need. |
| 97 | + |
| 98 | +CONSIDER THE SAME THING IN A MORE EMBEDDED CONTEXT |
| 99 | + |
| 100 | +## StackOverflow example |
| 101 | + |
| 102 | +From StackOverflow comes [a slightly more advanced example](https://stackoverflow.com/questions/76236450/jslt-are-there-global-variables). |
| 103 | + |
| 104 | +There is a defined ranking of keys: |
| 105 | + |
| 106 | +```let ranking = { |
| 107 | + "DEC": 0, |
| 108 | + "SBS": 1, |
| 109 | + "CON": 2, |
| 110 | + "GCS": 3, |
| 111 | + "GMS": 4, |
| 112 | + "FXP": 5, |
| 113 | + "QAN": 6, |
| 114 | + "REF": 7, |
| 115 | + "PRO": 8 |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +The input (presumably) looks something like: |
| 120 | + |
| 121 | +``` |
| 122 | +[ |
| 123 | + {..., "tag" : "CON"}, |
| 124 | + {..., "tag" : "REF"}, |
| 125 | + {..., "tag" : "DEC"}, |
| 126 | + ... |
| 127 | +] |
| 128 | +``` |
| 129 | + |
| 130 | +and we want the value with the lowest ranking. |
| 131 | + |
| 132 | +With an operator this would be: |
| 133 | + |
| 134 | +``` |
| 135 | +{ |
| 136 | + "data": { |
| 137 | + "segment": (.payload order by get-key($ranking, .tag)) [0] |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +whereas with a macro it would be: |
| 143 | + |
| 144 | +``` |
| 145 | +{ |
| 146 | + "data": { |
| 147 | + "segment": sort(.payload, get-key($ranking, .tag))[0] |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
0 commit comments