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/2584: refactored sort function #2630

Merged
merged 1 commit into from
Mar 29, 2024
Merged
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
11 changes: 6 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ export class Utils {
* Sorts array of nodes
* @param nodes array to sort
* @param dir 1 for asc, -1 for desc (optional)
* @param width width of the grid. If undefined the width will be calculated automatically (optional).
* @param column number of columns in the grid. If undefined columns will be calculated automatically (optional).
**/
static sort(nodes: GridStackNode[], dir: 1 | -1 = 1, column?: number): GridStackNode[] {
column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
if (dir === -1)
return nodes.sort((a, b) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
else
return nodes.sort((b, a) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
return nodes.sort((a, b) => {
let diffY = dir * (a.y - b.y);
Copy link
Member

@adumesny adumesny Mar 29, 2024

Choose a reason for hiding this comment

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

this is incorrect because x and y could be undefined (auto position) which is why I default to 1000 to be last, but still sorted relative to other items.
But looking at your code I realize I don't really need column at all since it's just relative and Y trumps anything else...

if (diffY === 0) return dir * column * (a.x - b.x);
return diffY;
});
}

/** find an item by id */
Expand Down