When implementing sorting algorithms for collections, ensure stable and predictable ordering to avoid inconsistencies from operations like Object.values(). Use multi-level sorting criteria and consider sequence-based approaches for complex ordering requirements.
Key principles:
Example implementation:
const sortModelTable = (
models: ReturnType<typeof collectModels>,
rule: "custom-first" | "default-first",
) =>
models.sort((a, b) => {
// Primary sort: custom vs default
let aIsCustom = a.provider?.providerType === "custom";
let bIsCustom = b.provider?.providerType === "custom";
if (aIsCustom !== bIsCustom) {
return aIsCustom === (rule === "custom-first") ? -1 : 1;
}
// Secondary sort: by provider type, then model name
if (a.provider && b.provider) {
const providerTypeComparison = a.provider.providerType.localeCompare(b.provider.providerType);
if (providerTypeComparison !== 0) {
return providerTypeComparison;
}
return a.name.localeCompare(b.name);
}
return a.name.localeCompare(b.name);
});
For even more stability, use explicit sequence numbers:
// Assign sequence numbers for predictable ordering
let customSeq = -1000; // Custom models get negative numbers (higher priority)
let defaultSeq = 1000; // Default models get positive numbers
const modelWithSorted = {
...model,
sorted: isCustom ? customSeq++ : defaultSeq++
};
This approach ensures consistent ordering regardless of the underlying data structure’s iteration order and provides clear, maintainable sorting logic.
Enter the URL of a public GitHub repository