|
1 |
| -import { isNumber, isString } from '@antv/util'; |
| 1 | +import { isFunction, isNumber, isString } from '@antv/util'; |
2 | 2 | import type {
|
3 | 3 | Edge,
|
4 | 4 | Graph,
|
@@ -127,33 +127,36 @@ export class GridLayout implements Layout<GridLayoutOptions> {
|
127 | 127 | (node) => cloneFormatData(node) as OutNode,
|
128 | 128 | );
|
129 | 129 |
|
130 |
| - if ( |
131 |
| - // `id` should be reserved keyword |
132 |
| - sortBy !== 'id' && |
133 |
| - (!isString(sortBy) || (layoutNodes[0] as any).data[sortBy] === undefined) |
134 |
| - ) { |
135 |
| - sortBy = 'degree'; |
136 |
| - } |
137 |
| - |
138 |
| - if (sortBy === 'degree') { |
| 130 | + if (isString(sortBy)) { |
| 131 | + if (sortBy === 'id') { |
| 132 | + // sort nodes by ID |
| 133 | + layoutNodes.sort(({ id: id1 }, { id: id2 }) => |
| 134 | + isNumber(id2) && isNumber(id1) |
| 135 | + ? id2 - id1 |
| 136 | + : `${id1}`.localeCompare(`${id2}`), |
| 137 | + ); |
| 138 | + } else if (sortBy === 'degree') { |
| 139 | + layoutNodes.sort( |
| 140 | + (n1, n2) => |
| 141 | + graph.getDegree(n2.id, 'both') - graph.getDegree(n1.id, 'both'), |
| 142 | + ); |
| 143 | + } else { |
| 144 | + // sort nodes by value |
| 145 | + layoutNodes.sort( |
| 146 | + (n1, n2) => n2.data[sortBy as string] - n1.data[sortBy as string], |
| 147 | + ); |
| 148 | + } |
| 149 | + } else if (isFunction(sortBy)) { |
| 150 | + // sort by custom function |
| 151 | + layoutNodes.sort(sortBy); |
| 152 | + } else { |
| 153 | + // sort nodes by degree |
139 | 154 | layoutNodes.sort(
|
140 | 155 | (n1, n2) =>
|
141 | 156 | graph.getDegree(n2.id, 'both') - graph.getDegree(n1.id, 'both'),
|
142 | 157 | );
|
143 |
| - } else if (sortBy === 'id') { |
144 |
| - // sort nodes by ID |
145 |
| - layoutNodes.sort((n1, n2) => { |
146 |
| - if (isNumber(n2.id) && isNumber(n1.id)) { |
147 |
| - return n2.id - n1.id; |
148 |
| - } |
149 |
| - return `${n1.id}`.localeCompare(`${n2.id}`); |
150 |
| - }); |
151 |
| - } else { |
152 |
| - // sort nodes by value |
153 |
| - layoutNodes.sort( |
154 |
| - (n1, n2) => (n2 as any).data[sortBy!] - (n1 as any).data[sortBy!], |
155 |
| - ); |
156 | 158 | }
|
| 159 | + |
157 | 160 | const width =
|
158 | 161 | !propsWidth && typeof window !== 'undefined'
|
159 | 162 | ? window.innerWidth
|
|
0 commit comments