mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
29 lines
798 B
TypeScript
29 lines
798 B
TypeScript
import type Collection from '#models/collection';
|
|
|
|
export default function sortCcollectionsByNextId(
|
|
collections: Collection[]
|
|
): Collection[] {
|
|
const sortedCollections: Collection[] = [];
|
|
|
|
const visit = (collection: Collection) => {
|
|
// Check if the collection has been visited
|
|
if (sortedCollections.includes(collection)) {
|
|
return;
|
|
}
|
|
|
|
// Visit the next collection recursively
|
|
const nextCollection = collections.find((c) => c.id === collection.nextId);
|
|
if (nextCollection) {
|
|
visit(nextCollection);
|
|
}
|
|
|
|
// Add the current collection to the sorted array
|
|
sortedCollections.push(collection);
|
|
};
|
|
|
|
// Visit each collection to build the sorted array
|
|
collections.forEach((collection) => visit(collection));
|
|
|
|
return sortedCollections.reverse();
|
|
}
|