Custom comparator for sorting in Starlark

152 views
Skip to first unread message

Christopher Kilian

unread,
Apr 18, 2022, 4:00:13 PM4/18/22
to bazel-discuss
Hello all,

I'm trying to sort something in a Bazel rule using a custom comparator. It looks like Starlark's sorted() only accepts a key, similar to python3. However, python3 has functool's cmp_to_key (https://www.geeksforgeeks.org/how-does-the-functools-cmp_to_key-function-works-in-python/) which essentially lets you use a comparator. Starlark doesn't allow for python imports, so this feature isn't available. The underlying implementation uses a class, which is also not available in Starlark, so recreating cmp_to_key in a .bzl file isn't an option.

Is there any way to use a custom comparator in Bazel? One option could be to write a simple insertion sort that uses the comparator, but this isn't ideal and I would prefer to use the built in sorted() function if it is possible.

Thanks!

Chris
Reply all
Reply to author
Forward
0 new messages