doc: sorting arrays
parent
3b5d56278f
commit
9478ff472f
18
doc/docs.md
18
doc/docs.md
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue