Sorting array of structs?

I have a construction like

Word :: struct {
   word: string,
   freq: u32,
}
words: [dynamic]Word
// Appending a couple of Words...

Can I use a function in core:sort to sort the words array with respect to freq or do I need to write a custom sort function?

1 Like

You need to write a compare function and pass it to slice.sort_by

This is the code:

word_order :: proc(lhs, rhs: Word) -> bool {
    return lhs.freq < rhs.freq
}
slice.sort_by(words[:], word_order)
5 Likes