Files
accounted/lib/articles/sort.ts
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

32 lines
1012 B
TypeScript

/**
* Canonical ordering for article lists and pickers: by article number with
* numeric-aware comparison ('2' before '10'), articles without a number last,
* name as tie-breaker. Users number their articles to control this order
* (issue #1053), so a plain string sort ('10' < '2') or name sort breaks it.
*/
interface SortableArticle {
article_number?: string | null
name: string
}
const numberCollator = new Intl.Collator('sv', { numeric: true, sensitivity: 'base' })
export function compareArticles(a: SortableArticle, b: SortableArticle): number {
const aNum = a.article_number?.trim()
const bNum = b.article_number?.trim()
if (aNum && bNum) {
const byNumber = numberCollator.compare(aNum, bNum)
if (byNumber !== 0) return byNumber
} else if (aNum) {
return -1
} else if (bNum) {
return 1
}
return numberCollator.compare(a.name, b.name)
}
export function sortArticles<T extends SortableArticle>(articles: T[]): T[] {
return [...articles].sort(compareArticles)
}