modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.17k stars 2.59k forks source link

[Feature Request] [stdlib] Add `argsort` to the builtin `sort` module #2979

Open martinvuyk opened 4 months ago

martinvuyk commented 4 months ago

Review Mojo's priorities

What is your request?

Add argsort similar to numpy's

What is your motivation for this change?

sorting inplace is not an option if you need to sort multiple arrays keeping the same permutations

Any other details?

I don't know what would be needed in terms of Span's capabilities. Because that permutation should be able to affect other items.

var my_list_distances = List[UInt64]()
var my_structures = List[MyStruct]()
...
var permutation = argsort(my_list_distances)
var ordered_structs = my_structures[permutation]

Or for now we can just return a list of indexes

JoeLoser commented 3 months ago

I agree we should provide some facilities to support sorting things beyond just in-place. This isn't currently a priority for the team, so I've put this as Postponed. We'll share more about our Q3 priorities soon to give some insights into how the community and us can build toward those goals together.