Currently, there is no easy way to get a sorted list of user's roles, despite the fact that GuildMember internally sorts the list of roles to get the role for :getHighestRole() and :getColor(). This Issue is actually two issues:
It would be nice for GuildMember to provide a method to get a sorted list of roles.
:getHighestRole() and :getColor() are doing unnecessary work when they sort the whole list (sorting is of O(n log n) complexity) just to get the highest role, for which a simple linear pass would work.
Just an idea here, but considering that :getRoles() (and thus all related functions) reference the role cache, a possible solution could be to have the cache always sorted (or always sort the result of State:getGuildRoles()), which would solve both problems.
For reference, my original comment from the community Discord server:
Yet another 3.0 suggestion: have GuildMember:getRoles() sort the roles according to their "height" from highest to lowest.
Reason: Having the list sorted can't hurt, and it's helpful for, say, printing out a list of a user's roles in order. The GuildMember class already has a local sorter defined that it uses for :getHighestRole() and :getColor() (also, what's the point of sorting the whole list (O(n log n) iirc) when a simple linear pass to get the highest role will suffice?), so implementing this should be trivial (heck, it could even go on and replace :getHighestRole()). Or maybe not have :getRoles() sort the list, but rather make a :getRolesSorted() function.
Currently, there is no easy way to get a sorted list of user's roles, despite the fact that
GuildMember
internally sorts the list of roles to get the role for:getHighestRole()
and:getColor()
. This Issue is actually two issues:GuildMember
to provide a method to get a sorted list of roles.:getHighestRole()
and:getColor()
are doing unnecessary work when they sort the whole list (sorting is of O(n log n) complexity) just to get the highest role, for which a simple linear pass would work.Just an idea here, but considering that
:getRoles()
(and thus all related functions) reference the role cache, a possible solution could be to have the cache always sorted (or always sort the result ofState:getGuildRoles()
), which would solve both problems.For reference, my original comment from the community Discord server: