Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list in Typst

Tags:

typst

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.

like image 456
Matt Joiner Avatar asked Oct 30 '25 06:10

Matt Joiner


1 Answers

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
]
like image 96
xkevio Avatar answered Nov 02 '25 22:11

xkevio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!