課程的檔案系統
實例 18 / 程式碼

sorting.js|公告排序完整程式

一個短小可讀的真實函式:鎖定、置頂、手動順序與發布時間如何形成學生看到的順序。

完整內容・去識別版程式碼32 行 · 全文
來源位置(邏輯別名)Google Drive / CourseWorkspace / live-board-mvp / src / sorting.js
來源版本/日期2026-09-11 來源副本
資料取得方式Google Drive 實際檔案/文件匯出
使用與備份界線這份檔案沒有服務帳密;它示範可獨立檢查的排序邏輯,不包含白板全部功能。

以下說明為本次整理;正文區保留來源的內容與歷史語氣。

閱讀實際內容 ↓下載 .js回到地圖位置
網址與識別碼處理本檔未偵測到需要替換的私人網址或識別碼。假網址一律使用 example.invalid,不能用來開啟原資料;請換成自己有權使用的來源。查看處理規則。
1const SORT_MODE_LABELS = {
2 newest: '最新發布在前',
3 oldest: '最舊發布在前',
4 manual: '教師手動排序',
5};
6
7export const SORT_MODES = Object.keys(SORT_MODE_LABELS);
8
9export function getSortModeLabel(mode) {
10 return SORT_MODE_LABELS[mode] || SORT_MODE_LABELS.newest;
13function timeValue(post) {
14 const value = post.publish_at || post.published_at || post.updated_at || post.created_at;
15 const time = value ? new Date(value).getTime() : 0;
16 return Number.isNaN(time) ? 0 : time;
19export function sortPosts(posts, mode = 'newest') {
20 return [...posts].sort((a, b) => {
21 const priorityDifference = Number(Boolean(b.pinned || b.locked)) - Number(Boolean(a.pinned || a.locked));
22 if (priorityDifference !== 0) return priorityDifference;
24 if (mode === 'manual') {
25 return (a.sort_order || 0) - (b.sort_order || 0) || timeValue(b) - timeValue(a);
26 }
28 const timeDifference = timeValue(b) - timeValue(a);
29 if (timeDifference !== 0) return mode === 'oldest' ? -timeDifference : timeDifference;
30 return (a.sort_order || 0) - (b.sort_order || 0);
31 });
資料依據:sorting.js,全文;來源版本 2026-09-11 來源副本;擷取/收錄 2026-09-13。
未公開原來源分享位置;此處的來源路徑是展示別名。
本收錄文字 SHA-256:ffa73021ebafa2ecaa18591821878ac262290343c2b7032563b5de549e8093f9
完整收錄清單與版本資訊