#Order arrayList?

1 messages · Page 1 of 1 (latest)

tired sparrow
#

I searched in the Zig documentation and found that Zig has several ways to sort things e.g. std.sort and std.mem.sort and others. I need to order an arrayList but don't know how to realise it. Any help is welcome.

limpid bane
#

use one of those functions on ArrayList.items

tired sparrow
#

Could you give an example with std.sort.desc() on someArray.items? Thanks so much in advance!

limpid bane
#

oh yeah thatd just be std.mem.sort(T, someArray.items, {}, std.sort.desc(T)); where T is your element type, {} is just an instance of void because the function returned by std.sort.desc takes void as it's context

tired sparrow
#

This was really helpful! I missed the void type {}.

limpid bane
#

np, {} actually just happens to evaluate to void because it's an empty block, but you can also do void{}

tired sparrow
#

Just one last questions, I can't slice the result with something like orderdArraylist[0..2]: error: slice of non-array type 'void'. Is there a way to extract specific slices, or do I need to convert the ordered arrayList?

limpid bane
limpid bane
tired sparrow
limpid bane
tired sparrow
limpid bane
keen forge
#

that seems like a not so good idea

limpid bane
#

although thats only if you need an unordered version too

tired sparrow
limpid bane
#

then just std.mem.sort(u32, someArray.items, {}, std.sort.desc(u32)) is all you need

#

someArray will be sorted after that line

#

the sorting functions change the underlying data in the list you give it to become sorted

tired sparrow
#

A very big thank you!