Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e830c8

Browse files
committed
fix/2584: refactored sort function
1 parent bf51a19 commit 9e830c8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,15 @@ export class Utils {
146146
* Sorts array of nodes
147147
* @param nodes array to sort
148148
* @param dir 1 for asc, -1 for desc (optional)
149-
* @param width width of the grid. If undefined the width will be calculated automatically (optional).
149+
* @param column number of columns in the grid. If undefined columns will be calculated automatically (optional).
150150
**/
151151
static sort(nodes: GridStackNode[], dir: 1 | -1 = 1, column?: number): GridStackNode[] {
152152
column = column || nodes.reduce((col, n) => Math.max(n.x + n.w, col), 0) || 12;
153-
if (dir === -1)
154-
return nodes.sort((a, b) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
155-
else
156-
return nodes.sort((b, a) => ((b.x ?? 1000) + (b.y ?? 1000) * column)-((a.x ?? 1000) + (a.y ?? 1000) * column));
153+
return nodes.sort((a, b) => {
154+
let diffY = dir * (a.y - b.y);
155+
if (diffY === 0) return dir * column * (a.x - b.x);
156+
return diffY;
157+
});
157158
}
158159

159160
/** find an item by id */

0 commit comments

Comments
 (0)