From 439418f2853a25312866260c3275bdaf978f2f89 Mon Sep 17 00:00:00 2001 From: denisk0m Date: Sun, 17 Nov 2024 01:23:53 +0200 Subject: [PATCH 1/2] raw sorting name --- src/utils/data/SortGroups.ts | 58 ++++++++++++++++++++++++++++++ src/utils/data/TimetableManager.ts | 3 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/utils/data/SortGroups.ts diff --git a/src/utils/data/SortGroups.ts b/src/utils/data/SortGroups.ts new file mode 100644 index 0000000..c07bcc2 --- /dev/null +++ b/src/utils/data/SortGroups.ts @@ -0,0 +1,58 @@ +interface Group { + fullname: string /*there store full name for example KB-111, + PZ-23, МЕОА-11f(23/24н.р.) and so on*/; + numberForSort: number /* there will store number + of group we need for sorting. In group where number is 23 we will + add 0 like 203 for correct sorting */; +} +//return sorted array of Group +const sortGroup = (groups: Group[]): Group[] => { + return groups.sort((a, b) => a.numberForSort - b.numberForSort); +}; +//return "PZ" if PZ-24, or LMP if LMP-5124 +const findPrefixOfGroup = (group: string): string => { + const firstDashIndex = group.indexOf("-"); + return group.slice(0, firstDashIndex); +}; +//return 203 if PZ-23, or 111 if IO-111faf +const extractNumberFromGroup = (input: string): number | null => { + const match = input.match(/-(\d+)/); // Regular expression to find a dash followed by digits + let str = match[1]; + //add 0 to xx number + if (str.length === 2) { + str = `${str.slice(0, 1)}0${str.slice(1)}`; + } + return match ? Number(str) : null; // Return the digits if a match is found, otherwise null +}; +export const sortingGroupsArray = (groups: string[]): string[] => { + const map = new Map(); + for (const group of groups) { + const prefixGroup: string = findPrefixOfGroup(group); + const numberGroup: number | null = extractNumberFromGroup(group); + if (!numberGroup) { + console.log("there is error in extracting group number in ", group); + return []; + } + const instance: Group = { fullname: group, numberForSort: numberGroup }; + if (!map.has(prefixGroup)) { + //if there is not such group prefix + map.set(prefixGroup, [instance]); + } else { + const oldValue: Group[] = map.get(prefixGroup); + map.set(prefixGroup, [...oldValue, instance]); + } + } + //sort each group in prefix map separately + for (const key of map.keys()) { + const groups: string[] = sortGroup(map.get(key)); + map.set(key, groups); + } + const resultGroups: string[] = []; + for (const key of map.keys()) { + for (const group of map.get(key)) { + resultGroups.push(group.fullname); + } + } + + return resultGroups; +}; diff --git a/src/utils/data/TimetableManager.ts b/src/utils/data/TimetableManager.ts index cced1be..7920616 100644 --- a/src/utils/data/TimetableManager.ts +++ b/src/utils/data/TimetableManager.ts @@ -9,6 +9,7 @@ import { type TimetableType, } from "@/types/timetable"; import type { ActualPromise, OptimisticPromise, RenderPromises } from "@/types/utils"; +import { sortingGroupsArray } from "@/utils/data/SortGroups"; import { DEVELOP } from "../constants"; import storage from "../storage"; import * as Util from "../timetable"; @@ -63,7 +64,7 @@ class TimetableManager { }); // request data from server if needed - this.groups = await this.getData(GROUPS, FallbackData.getGroups); + this.groups = sortingGroupsArray(await this.getData(GROUPS, FallbackData.getGroups)); this.selectiveGroups = await this.getData(SELECTIVE_GROUPS, FallbackData.getSelectiveGroups); this.lecturers = await this.getData(LECTURERS, FallbackData.getLecturers); } From 6926c8d3917520192b342467736be26a5e0c64e7 Mon Sep 17 00:00:00 2001 From: cupoftea4 Date: Sat, 23 Nov 2024 16:50:07 +0200 Subject: [PATCH 2/2] [refactor] simplify sorting method --- src/utils/data/SortGroups.ts | 58 ------------------------------ src/utils/data/TimetableManager.ts | 6 ++-- src/utils/timetable.ts | 9 +++++ 3 files changed, 12 insertions(+), 61 deletions(-) delete mode 100644 src/utils/data/SortGroups.ts diff --git a/src/utils/data/SortGroups.ts b/src/utils/data/SortGroups.ts deleted file mode 100644 index c07bcc2..0000000 --- a/src/utils/data/SortGroups.ts +++ /dev/null @@ -1,58 +0,0 @@ -interface Group { - fullname: string /*there store full name for example KB-111, - PZ-23, МЕОА-11f(23/24н.р.) and so on*/; - numberForSort: number /* there will store number - of group we need for sorting. In group where number is 23 we will - add 0 like 203 for correct sorting */; -} -//return sorted array of Group -const sortGroup = (groups: Group[]): Group[] => { - return groups.sort((a, b) => a.numberForSort - b.numberForSort); -}; -//return "PZ" if PZ-24, or LMP if LMP-5124 -const findPrefixOfGroup = (group: string): string => { - const firstDashIndex = group.indexOf("-"); - return group.slice(0, firstDashIndex); -}; -//return 203 if PZ-23, or 111 if IO-111faf -const extractNumberFromGroup = (input: string): number | null => { - const match = input.match(/-(\d+)/); // Regular expression to find a dash followed by digits - let str = match[1]; - //add 0 to xx number - if (str.length === 2) { - str = `${str.slice(0, 1)}0${str.slice(1)}`; - } - return match ? Number(str) : null; // Return the digits if a match is found, otherwise null -}; -export const sortingGroupsArray = (groups: string[]): string[] => { - const map = new Map(); - for (const group of groups) { - const prefixGroup: string = findPrefixOfGroup(group); - const numberGroup: number | null = extractNumberFromGroup(group); - if (!numberGroup) { - console.log("there is error in extracting group number in ", group); - return []; - } - const instance: Group = { fullname: group, numberForSort: numberGroup }; - if (!map.has(prefixGroup)) { - //if there is not such group prefix - map.set(prefixGroup, [instance]); - } else { - const oldValue: Group[] = map.get(prefixGroup); - map.set(prefixGroup, [...oldValue, instance]); - } - } - //sort each group in prefix map separately - for (const key of map.keys()) { - const groups: string[] = sortGroup(map.get(key)); - map.set(key, groups); - } - const resultGroups: string[] = []; - for (const key of map.keys()) { - for (const group of map.get(key)) { - resultGroups.push(group.fullname); - } - } - - return resultGroups; -}; diff --git a/src/utils/data/TimetableManager.ts b/src/utils/data/TimetableManager.ts index 7920616..ae35f90 100644 --- a/src/utils/data/TimetableManager.ts +++ b/src/utils/data/TimetableManager.ts @@ -9,7 +9,7 @@ import { type TimetableType, } from "@/types/timetable"; import type { ActualPromise, OptimisticPromise, RenderPromises } from "@/types/utils"; -import { sortingGroupsArray } from "@/utils/data/SortGroups"; +import { sortGroups } from "@/utils/timetable"; import { DEVELOP } from "../constants"; import storage from "../storage"; import * as Util from "../timetable"; @@ -64,8 +64,8 @@ class TimetableManager { }); // request data from server if needed - this.groups = sortingGroupsArray(await this.getData(GROUPS, FallbackData.getGroups)); - this.selectiveGroups = await this.getData(SELECTIVE_GROUPS, FallbackData.getSelectiveGroups); + this.groups = sortGroups(await this.getData(GROUPS, FallbackData.getGroups)); + this.selectiveGroups = sortGroups(await this.getData(SELECTIVE_GROUPS, FallbackData.getSelectiveGroups)); this.lecturers = await this.getData(LECTURERS, FallbackData.getLecturers); } diff --git a/src/utils/timetable.ts b/src/utils/timetable.ts index 0f02e1c..3b52704 100644 --- a/src/utils/timetable.ts +++ b/src/utils/timetable.ts @@ -178,3 +178,12 @@ export function generateSaturdayLessons(originalTimetable: TimetableItem[]) { } return []; } + +export function sortGroups(groups: string[]) { + return groups.toSorted((g1, g2) => { + const r = /^(.*-\d)(\d+).*$/; + const [, year1 = "", groupNumber1 = ""] = g1.split(r); + const [, year2 = "", groupNumber2 = ""] = g2.split(r); + return `${year1}${groupNumber1.padStart(2, "0")}`.localeCompare(`${year2}${groupNumber2.padStart(2, "0")}`); + }); +}