Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly sort roles for Role#position #3107

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/structures/Guild.js
Expand Up @@ -982,7 +982,7 @@ class Guild extends Base {
* @private
*/
_sortedRoles() {
return Util.discordSort(this.roles);
return Util.discordSort(this.roles, (a, b) => b.rawPosition - a.rawPosition);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/structures/Role.js
Expand Up @@ -378,8 +378,10 @@ class Role extends Base {
* positive number if the first's is higher (second's is lower), 0 if equal
*/
static comparePositions(role1, role2) {
if (role1.position === role2.position) return role2.id - role1.id;
return role1.position - role2.position;
const { position: position1 } = role1;
const { position: position2 } = role2;
if (position1 === position2) return role2.id - role1.id;
return position2 - position1;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/util/Util.js
Expand Up @@ -296,12 +296,13 @@ class Util {

/**
* Sorts by Discord's position and ID.
* @param {Collection} collection Collection of objects to sort
* @param {Collection} collection Collection of objects to sort
* @param {Function} [positionSort] Function that determines the position order to sort as
* @returns {Collection}
*/
static discordSort(collection) {
static discordSort(collection, positionSort = (a, b) => a.rawPosition - b.rawPosition) {
return collection.sort((a, b) =>
a.rawPosition - b.rawPosition ||
positionSort(a, b) ||
parseInt(b.id.slice(0, -10)) - parseInt(a.id.slice(0, -10)) ||
parseInt(b.id.slice(10)) - parseInt(a.id.slice(10))
);
Expand Down