Sort a list in Typst

213 Views Asked by At

I have a list like

- 3
- 2
- 1

I'd like to sort it before displaying. How do I do that?

I've tried variations of #show list,

#let a = [
  - 3
  - 2
  - 1
]
#a.children.sorted()

I can't quite work how how to hook in to what I want here.

1

There are 1 best solutions below

0
On

There's two ways you can do this. First, you probably want to save your list data in an array independently of the list type. Then you can sort it and spread the sorted list with the ..-operator into #list.

If your data consists of integers, you will have to convert it to content first currently. The second approach is using a for-loop, no spreading or mapping required.

#let a = (3, 2, 1)
#let b = a.sorted().map(x => [#x])

#list(..b)

#for x in a.sorted() [
  - #x
]