21 lines
743 B
JavaScript
21 lines
743 B
JavaScript
export function sortByField(from, to, queryRecords, fieldName) {
|
|
if (from === to) {
|
|
return queryRecords;
|
|
}
|
|
let edgesTosort = queryRecords?.filter((qr) => qr.edge.field === fieldName && qr.edge.depth === 1 ) ?? [];
|
|
let remainingEdge = queryRecords?.filter((qr) => !(qr.edge.field === fieldName && qr.edge.depth === 1)) ?? [];
|
|
|
|
edgesTosort = array_move(edgesTosort,from, to);
|
|
return [...remainingEdge, ...edgesTosort];
|
|
}
|
|
|
|
function array_move(arr, old_index, new_index) {
|
|
if (new_index >= arr.length) {
|
|
var k = new_index - arr.length + 1;
|
|
while (k--) {
|
|
arr.push(undefined);
|
|
}
|
|
}
|
|
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
|
|
return arr; // for testing
|
|
}; |