Skip to content

Commit aa12293

Browse files
Merge pull request #7002 from christianbeeznest/GH-1655-3
Catalogue: Fix natural order for course titles (ASC by default)
2 parents b1df01a + 7fd1f3e commit aa12293

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

assets/vue/views/course/CatalogueCourses.vue

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ import AdvancedCourseFilters from "../../components/course/AdvancedCourseFilters
110110
const { t } = useI18n()
111111
const sortField = ref("title")
112112
113+
const natural = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" })
113114
const router = useRouter()
114115
const securityStore = useSecurityStore()
115116
const platformConfigStore = usePlatformConfig()
@@ -451,13 +452,23 @@ const visibleCoursesBase = computed(() => {
451452
valB = b.extra_fields?.[field] ?? ""
452453
}
453454
454-
if (typeof valA === "string") valA = valA.toLowerCase()
455-
if (typeof valB === "string") valB = valB.toLowerCase()
456-
457-
if (valA < valB) return -1 * order
458-
if (valA > valB) return 1 * order
459-
return 0
455+
// Natural compare for strings; numeric compare otherwise
456+
const cmp =
457+
typeof valA === "string" || typeof valB === "string"
458+
? natural.compare(String(valA), String(valB))
459+
: valA < valB
460+
? -1
461+
: valA > valB
462+
? 1
463+
: 0
464+
465+
return (
466+
cmp *
467+
(typeof order === "number" ? (order >= 0 ? 1 : -1) : String(order).toLowerCase().startsWith("desc") ? -1 : 1)
468+
)
460469
})
470+
} else {
471+
list = list.slice().sort((a, b) => natural.compare(String(a.title ?? ""), String(b.title ?? "")))
461472
}
462473
463474
return list

0 commit comments

Comments
 (0)