# Ordering functions

BQN has six functions that order arrays as part of their operation (the comparison functions `β€<>β₯` only order atoms, so they aren't included). These come in three pairs, where one of each pair uses an ascending ordering and the other uses a descending ordering.

• `β¨β§`, Sort, puts major cells of `π©` in order
• `ββ`, Grade, outputs the permutation that Sort would use to rearrange `π©`
• `ββ`, Bins, takes an ordered `π¨` and determines where each cell of `π©` fits in this ordering.

The array ordering shared by all six is described last. For lists it's "dictionary ordering": two lists are compared one element at a time until one runs out, and the shorter one comes first in case of a tie. Operation values aren't ordered, so if an argument to an ordering function has a function or modifier somewhere in it then it will fail unless all the orderings can be decided without checking that value.

You can't provide a custom ordering function to Sort. The function would have to be called on one pair of cells at a time, which is contrary to the idea of array programming, and passing in a function with side effects could lead to implementation-specific behavior. Instead, build another array that will sort in the order you want (for example, by selecting or deriving the property you want to sort on). Then Grade it, and use the result to select from the original array.

## Sort

You've probably seen it before. Sort Up (`β§`) reorders the major cells of its argument to place them in ascending order, and Sort Down (`β¨`) puts them in descending order. Every ordering function follows this naming conventionβthere's an "Up" version pointing up and a "Down" version going the other way.

βοΈ
```    β§ "delta"βΏ"alpha"βΏ"beta"βΏ"gamma"
β¨ "alpha" "beta" "delta" "gamma" β©

β¨ "Ξ΄Ξ±Ξ²Ξ³"
"Ξ΄Ξ³Ξ²Ξ±"
```

Sort Down always matches Sort Up reversed, `β½ββ§`. The reason for this is that BQN's array ordering is a total order, meaning that if one array doesn't come earlier or later than another array in the ordering then the two arrays match. Since any two non-matching argument cells are strictly ordered, they will have one ordering in `β§` and the opposite ordering in `β¨`. After the reverse, any pair of non-matching cells are ordered the same way in `β½ββ§` and `β¨`. Since these two results have the same major cells in the same order, they match. However, note that the results will not always behave identically because Match doesn't take fill elements into account (if you're curious, take a look at `1βΒ¨β¨β¨β0,""β©` versus `1βΒ¨β½ββ§β¨β0,""β©`).

Grade is more abstract than Sort. Rather than rearranging the argument's cells immediately, it returns a list of indices (more precisely, a permutation) giving the ordering that would sort them.

βοΈ
```    β’ l β "planet"βΏ"moon"βΏ"star"βΏ"asteroid"
β¨ "planet" "moon" "star" "asteroid" β©

β§ l
β¨ "asteroid" "moon" "planet" "star" β©

β l
β¨ 3 1 0 2 β©
```

Given our list `l` of things in a solar system, Sort Up orders them by size, or maybe alphabetically. What does `βl` do? Its result also orders these items, but instead of listing them directly, each element is the index of that cell in the argument. So the way to read it is that the first item in sorted order is cell `3` of the argument, `"asteroid"`. The second is cell `1`, `"moon"`, and the thirdβforget this, we made programming languages for a reason.

βοΈ
```    (βl) β l
β¨ "asteroid" "moon" "planet" "star" β©
```

### Ordinals

So the elements of the Grade of an array correspond to the cells of that array after it's sorted. It's tempting if you don't have the sorted list handy to try to match them up with major cells of the original array, but this never makes senseβthere's no relationship. However, applying Grade twice gives us a list that does correspond to the original argument quite usefully: it says, for each major cell of that argument, what rank it has relative to the others (smallest is 0, next is 1, and so on, breaking ties in favor of which cell comes earlier in the argument). Experienced APL programmers call this pattern the "ordinals" idiom.

βοΈ
```    l β ββ l
ββ
β΅ "planet" "moon" "star" "asteroid"
2        1      3      0
β
```

How does it work? First, let's note that `βl` is a permutation: it contains exactly the numbers `ββ l`, possibly in a different order. In other words, `β§βl` is `ββ l`. Permuting an array rearranges the cells but doesn't remove or duplicate any. This implies it's always invertible: given a permutation `p`, some other permutation `q` will have `π© β‘ qβpβπ©` for every `π©` of the right length. This would mean that while `βl` transforms `l` to `β§l`, the inverse of `βl` transforms `β§l` back into `l`. That's what we want: for each cell of `l`, the corresponding number in the inverse of `βl` is what index that cell has after sorting.

But what's the inverse `q` of a permutation `p`? Our requirement is that `π© β‘ qβpβπ©` for any `π©` with the same length as `p`. Setting `π©` to `ββ p` (the identity permutation), we have `(ββ p) β‘ qβp`, because `pβββ p` is just `p`. But if `p` is a permutation then `β§p` is `ββ p`, so our requirement could also be written `(β§p) β‘ qβp`. Now it's all coming back around again. We know exactly how to get `q`! Defining `qββp`, we have `qβp β (βp)βp β β§p β ββ p`, and `qβpβπ© β (qβp)βπ© β (ββ p)βπ© β π©`.

The fact that Grade Up inverts a permutation is useful in itself. Note that this applies to Grade Up specifically, and not Grade Down. This is because the identity permutation is ordered in ascending order. Grade Down would invert the reverse of a permutation, which is unlikely to be useful. So the ordinals idiom that goes in the opposite direction is actually not `ββ` but `ββ`. The initial grade is different, but the way to invert it is the same.

### Stability

When sorting an array, we usually don't care how matching cells are ordered relative to each other (although as mentioned above it's possible to detect it by using fill elements carefully. They maintain their ordering). Grading is a different matter, because often the grade of one array is used to order another one.

βοΈ
```    β’ t β [ "dog"βΏ4, "ant"βΏ6, "pigeon"βΏ2, "pig"βΏ4 ]
ββ
β΅ "dog"    4
"ant"    6
"pigeon" 2
"pig"    4
β

1 βΛ t
β¨ 4 6 2 4 β©

(1βΛt) ββΈβ t
ββ
β΅ "pigeon" 2
"dog"    4
"pig"    4
"ant"    6
β
```

Here we order a table by its second column. Maybe in this case it's not a problem if "dog" and "pig" trade places. But unpredictability is never goodβwould you get the same results with a different implementation of BQN? And for many other applications of Grade the ordering of equal elements is important. So BQN specifies that matching cells are always ordered by their indices. The same rule applies for Grade Down, so that for example the grade in either direction of an array `π©` where all cells are the same is `ββ π©`. One effect is that `βπ©` is not always the same as `β½βπ©`, even though `β§π©` always matches `β½β¨π©`. And in the table below we can see that the numbers are all reversed but "dog" and "pig" stay in the same order.

βοΈ
```    (1βΛt) ββΈβ t
ββ
β΅ "ant"    6
"dog"    4
"pig"    4
"pigeon" 2
β
```

To see some of the possibilities of Grade, you might pick apart the following expression, which is used to reverse elements of the right argument in groups of the given length.

βοΈ
```    (β½β/3βΏ4βΏ5) β "012abcdABCDE"
"210dcbaEDCBA"
```

## Bins

The two Bins functions are written with the same symbols `β` and `β` as Grade, but take two arguments instead of one. More complicated? A little, but once you understand Bins you'll find that it's a basic concept that shows up in the real world all the time.

Bins behaves like a search function with respect to rank: it looks up cells from `π©` relative to major cells of `π¨`. However, there's an extra requirement: the left argument to Bins must already be sorted according to whichever ordering is used. If it isn't, you'll get an error.

βοΈ
```    5βΏ6βΏ2βΏ4βΏ1 β 3
Error: β: π¨ must be sorted

0βΏ3βΏ4βΏ7βΏ9 β 3
Error: β: π¨ must be sorted in descending order
```

Given this, the simplest definition of `π¨βπ©` (or `π¨βπ©`) is that for each cell in `π©` of rank `(=π¨)-1`, it counts the number of major cells from `π¨` that come earlier in the ordering, or match that cell.

Why would that be useful? How about an example. A pinball machine has some high scores on it. You play, and your rank is the number of scores higher than yours (in this case, if you tie someone's score, you won't unseat them).

βοΈ
```    hs β 1e7Γ627βΏ581βΏ578βΏ553βΏ520  # High scores

hs β 1e7Γ565βΏ322βΏ788βΏ627
β¨ 3 5 0 1 β©
```

A score of `565e7` sits between `578e7` and `553e7` at rank 3, `322e7` wouldn't make the list, `788e7` would beat everyone, and `627e7` would tie the high score but not beat it. The same principles apply to less spring-loaded things like character indices and line numbers (`π¨` is the index of the start of each line), or percentage scores and letter grades on a test (`π¨` is the minimum score possible for each grade). In each case, it's better to think of Bins not as a counting exercise but as finding "what bin" something fits into.

## Array ordering

Most of the time you won't need to worry about the details of how BQN arrays are ordered. It's documented here because, well, that's what documentation does.

BQN's array ordering is an extension of the number and character ordering given by `β€` to arrays. In this system, any two arrays that have only numbers and characters for atoms can be compared with each other. Furthermore, some arrays that contain incomparable atoms (operations or namespaces) might be comparable, if the result of the comparison can be decided before reaching these atoms. Array ordering never depends on fill elements. If comparing two arrays succeeds, there are three possibilities: the first array is smaller, the second is smaller, or the two arrays match. All of the "Up" ordering functions use this ordering directly, so that smaller arrays come earlier, and the "Down" ones use the opposite ordering, with larger arrays coming earlier.

Comparing two atoms is defined to work the same way as the comparison functions `β€<>β₯`. Numbers come earlier than characters and otherwise these two types are ordered in the obvious way. To compare an atom to an array, the atom is enclosed and then compared with the array ordering defined below. The result of this comparison is used except when the two arrays match: in that case, the atom is considered smaller.

Two arrays of the same shape are compared by comparing all their corresponding elements, in index order. This comparison stops at the first pair of different elements (which allows later elements to contain operations without causing an error). If any elements were different, then they decide the result of the comparison. If all the elements matched, then by definition the two arrays match.

The principle for arrays of different shapes is the same, but there are two factors that need to be taken into account. First, it's not obvious any more what it means to compare corresponding elementsβwhat's the correspondence? Second, the two arrays can't match because they have different shapes. So even if all elements end up matching one of them needs to come earlier.

Let's discuss correspondence first. One way to think about how BQN makes arrays correspond is that they're simply laid on top of each other, lining up the first (as in `β`) elements. So a shape `β¨4β©` array will match up with the first row of a shape `5βΏ3` array, but have an extra element off the end. A simple way to think about this is to say that the lower rank array is brought up to a matching rank by putting `1`s in front of the shape, and then lengths along each axis are matched up by padding the shorter array along that axis with a special "nothing" element. This "nothing" element will be treated as smaller than any actual array, because this rule recovers the "dictionary ordering" rule that a word that's a prefix of a longer word comes before that word. In the case of the shapes `β¨4β©` and `5βΏ3`, if the three overlapping elements match then the fourth element comes from the first row and is present in the first array but not the second. So the shape `5βΏ3` array would be considered smaller without even looking at its other four rows.

It can happen that two arrays of different shape have all matching elements with this procedure: either because one array's shape is the same as the other's but with some extra `1`s at the beginning, or because both arrays are empty. In this case, the arrays are compared first by rank, with the higher-rank array considered larger, and then by shape, beginning with the leading axes.