Skip to content

Commit eceacf0

Browse files
committed
Fix sorting of components with single member
1 parent 32610a7 commit eceacf0

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

etc/js/components/widgets/table/entity-table.vue

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,45 @@ const orderByIndices = computed(() => {
197197
let aValue = a.value;
198198
let bValue = b.value;
199199
200-
if (typeof aValue === 'number' && typeof bValue === 'number') {
201-
return (aValue - bValue) - (bValue - aValue);
200+
// If both elements are objects with a first member of a number type, sort
201+
// on the first member so we don't compare numbers as strings.
202+
if (typeof aValue === 'object' && typeof bValue === 'object') {
203+
const aKeys = Object.keys(aValue);
204+
const bKeys = Object.keys(bValue);
205+
if (aKeys.length && bKeys.length) {
206+
const aFirst = aValue[aKeys[0]];
207+
const bFirst = bValue[bKeys[0]];
208+
209+
if (typeof aFirst === 'number' && typeof bFirst === 'number') {
210+
aValue = aFirst;
211+
bValue = bFirst;
212+
}
213+
}
202214
}
203215
204-
if (typeof aValue === 'number') {
205-
aValue = "" + aValue;
206-
}
207-
if (typeof bValue === 'number') {
208-
bValue = "" + bValue;
209-
}
216+
let comp;
210217
211-
if (typeof aValue === 'object') {
212-
aValue = JSON.stringify(aValue);
213-
}
214-
if (typeof bValue === 'object') {
215-
bValue = JSON.stringify(bValue);
218+
if (typeof aValue === 'number' && typeof bValue === 'number') {
219+
comp = (aValue - bValue) - (bValue - aValue);
220+
} else {
221+
222+
if (typeof aValue === 'number') {
223+
aValue = "" + aValue;
224+
}
225+
if (typeof bValue === 'number') {
226+
bValue = "" + bValue;
227+
}
228+
229+
if (typeof aValue === 'object') {
230+
aValue = JSON.stringify(aValue);
231+
}
232+
if (typeof bValue === 'object') {
233+
bValue = JSON.stringify(bValue);
234+
}
235+
236+
comp = aValue.localeCompare(bValue);
216237
}
217238
218-
let comp = aValue.localeCompare(bValue);
219239
if (orderBy.value.mode === 'desc') {
220240
comp *= -1;
221241
}

0 commit comments

Comments
 (0)