Skip to content

Commit

Permalink
fix: default sorting logic in student list
Browse files Browse the repository at this point in the history
- Ensure consistent sorting behavior between browser
  • Loading branch information
U1805 committed Jan 29, 2025
1 parent b38ce65 commit 471ed09
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ const processData = debounce(() => {
// sort
.sort((a, b) => {
if (filter_condition.value.sort_type === '') {
return filter_condition.value.sort_asc ? -1 : 1
return filter_condition.value.sort_asc ?
database.value.indexOf(a) - database.value.indexOf(b) :
database.value.indexOf(b) - database.value.indexOf(a)
}
if (filter_condition.value.sort_type === 'Birthday') {
Expand Down
13 changes: 7 additions & 6 deletions src/assets/requestUtils/dateFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export const dateFormatReverse = (date: string, lng: SupportedLanguage): string

export const birthday_sort = (a: studentInfo, b: studentInfo, lng: SupportedLanguage) => {
// 转换为今年的日期以进行比较
const today = new Date()
const currentYear = today.getFullYear()
const now = new Date()
const today = now.setHours(0, 0, 0, 0)
const currentYear = now.getFullYear()

const getDateFromBirthday = (birthday: string) => {
birthday = dateFormatReverse(birthday, lng)
Expand All @@ -73,22 +74,22 @@ export const birthday_sort = (a: studentInfo, b: studentInfo, lng: SupportedLang
return yesterday
}
const [month, day] = birthday.split('/').map(Number)
return new Date(currentYear, month - 1, day - 1)
return new Date(currentYear, month - 1, day)
}

const aDate = getDateFromBirthday(a.Birthday)
const bDate = getDateFromBirthday(b.Birthday)

// ========================= Flag Birthday Start =========================
const diffDays = Math.ceil((aDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24))
const diffDays = Math.ceil((aDate.getTime() - today) / (1000 * 60 * 60 * 24))
if (diffDays >= 0 && diffDays < 3) { // 检查是否未来3天内过生日
a.School = a.School.replaceAll("-Birthday", "") + "-Birthday"
}
// ========================= Flag Birthday End =========================

// 如果日期已经过了,使用明年的日期
if (aDate < today) aDate.setFullYear(currentYear + 1)
if (bDate < today) bDate.setFullYear(currentYear + 1)
if (aDate.getTime() < today) aDate.setFullYear(currentYear + 1)
if (bDate.getTime() < today) bDate.setFullYear(currentYear + 1)

return aDate.getTime() - bDate.getTime()
}

0 comments on commit 471ed09

Please sign in to comment.