🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@MaanasArora
Copy link
Contributor

@MaanasArora MaanasArora commented Dec 11, 2025

Follows #29737 and #30328 (and supersedes #29987) to implement a sort_compare slot that can be used to register sorting methods using a comparison function. This functionality also existed in the ArrFuncs slots, but now we implement the new-style sorting methods, and the array method context is passed to the comparison function rather than the array itself.

I've tested this with mpfdtype on the user dtypes repo (though having a bit of an issue with installation... the slot wasn't visible there, so had to redefine just the macro). Unfortunately there don't seem to be dtypes we can write tests for within numpy itself, though I did a small experiment with the scaled float dtype. ping @seberg @mhvk @ngoldbaum if you're interested - thanks!

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhvk and @charris I think had strong opinions about prior versions of this. I'd appreciate it if they could weigh in on the approach.

Overall I think this is a nice cleanup and since StringDType wanted it, I wouldn't be surprised if future DTypes do as well.

If defined, implements a comparison function for two array elements
for use in sorting and argsorting. This can be defined in place of the
custom sort functions using the ArrayMethod API (see :ref:`array-methods-sorting`)
to implement sorting for the DType.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a mention for why you'd want to do this - e.g. if comparisons have some big cost that can be amortized over the whole array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a note - a bit on the detailed side, hopefully that works, thanks!

@MaanasArora
Copy link
Contributor Author

Thanks for looking @ngoldbaum!

Overall I think this is a nice cleanup and since StringDType wanted it, I wouldn't be surprised if future DTypes do as well.

Agreed, the full loop implementation would probably be overkill in many cases. I think this would be necessary to expose the sorts on the user-dtypes too since they don't define full sorts now, so a good way to test the new infrastructure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants