doc: sorting arrays

pull/6117/head
Alexander Medvednikov 2020-08-12 19:14:13 +02:00
parent 3b5d56278f
commit 9478ff472f
1 changed files with 32 additions and 14 deletions

View File

@ -558,6 +558,24 @@ a[0][1][1] = 2
println(a) // [[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
```
#### Sorting arrays
Sorting arrays of all kinds is very simple and intuitive. Special variables `a` and `b`
are used when providing a custom sorting condition.
```v
mut numbers := [1, 3, 2]
numbers.sort() // 1, 2, 3
numbers.sort(a > b) // 3, 2, 1
```
```v
struct User { age int name string }
mut users := [...]
users.sort(a.age < b.age) // sort by User.age int field
users.sort(a.name > b.name) // reverse sort by User.name string field
```
### Maps
```v