Skip to content

Commit 54a78ad

Browse files
committed
fix: Implemented natural numeric sorting for departments, batches, etc.
1 parent 4044149 commit 54a78ad

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/lib/firebase/firestore.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ const FOLDERS = "folders";
2424
export const getDepartments = async () => {
2525
const q = query(collection(db, DEPARTMENTS), orderBy("name"));
2626
const snapshot = await getDocs(q);
27-
return snapshot.docs.map(d => ({ id: d.id, ...d.data() }));
27+
// Client-side sort with numeric option
28+
return snapshot.docs
29+
.map(d => ({ id: d.id, ...d.data() }))
30+
.sort((a: any, b: any) => (a.name || "").localeCompare(b.name || "", undefined, { numeric: true }));
2831
};
2932

3033
export const createDepartment = (name: string) =>
@@ -42,7 +45,7 @@ export const getBatches = async (departmentId: string) => {
4245
// Client-side sort to avoid composite index requirement
4346
return snapshot.docs
4447
.map(d => ({ id: d.id, ...d.data() }))
45-
.sort((a: any, b: any) => (a.name || "").localeCompare(b.name || ""));
48+
.sort((a: any, b: any) => (a.name || "").localeCompare(b.name || "", undefined, { numeric: true }));
4649
};
4750

4851
export const createBatch = (departmentId: string, name: string) =>
@@ -58,7 +61,7 @@ export const getSemesters = async (batchId: string) => {
5861
const q = query(collection(db, SEMESTERS), where("batchId", "==", batchId));
5962
const snapshot = await getDocs(q);
6063
// Sort manually or add composite index
61-
return snapshot.docs.map(d => ({ id: d.id, ...d.data() })).sort((a: any, b: any) => a.name.localeCompare(b.name));
64+
return snapshot.docs.map(d => ({ id: d.id, ...d.data() })).sort((a: any, b: any) => a.name.localeCompare(b.name, undefined, { numeric: true }));
6265
};
6366

6467
export const createSemester = (batchId: string, name: string) =>
@@ -73,7 +76,7 @@ export const deleteSemester = (id: string) => deleteDoc(doc(db, SEMESTERS, id));
7376
export const getSubjects = async (semesterId: string) => {
7477
const q = query(collection(db, SUBJECTS), where("semesterId", "==", semesterId));
7578
const snapshot = await getDocs(q);
76-
return snapshot.docs.map(d => ({ id: d.id, ...d.data() })).sort((a: any, b: any) => a.name.localeCompare(b.name));
79+
return snapshot.docs.map(d => ({ id: d.id, ...d.data() })).sort((a: any, b: any) => a.name.localeCompare(b.name, undefined, { numeric: true }));
7780
};
7881

7982
export const createSubject = (semesterId: string, name: string) =>
@@ -90,7 +93,7 @@ export const getFolders = async (subjectId: string) => {
9093
const snapshot = await getDocs(q);
9194
return snapshot.docs
9295
.map(d => ({ id: d.id, ...d.data() }))
93-
.sort((a: any, b: any) => (a.name || "").localeCompare(b.name || ""));
96+
.sort((a: any, b: any) => (a.name || "").localeCompare(b.name || "", undefined, { numeric: true }));
9497
};
9598

9699
export const createFolder = (data: any) =>

0 commit comments

Comments
 (0)