From 2bfe7ec85527cf77cd8cbd285336700dcdbc89d1 Mon Sep 17 00:00:00 2001 From: arch-spatula Date: Thu, 12 Jan 2023 16:56:06 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=8B=9C=EA=B0=84=EC=88=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A0=AC=20#1=20#2=20#5=20#14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AM PM 구분하고 정렬합니다. 그 다음 시간에 따라 순서를 정렬합니다. --- src/Hooks/usePill.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Hooks/usePill.js b/src/Hooks/usePill.js index e85457c..656b442 100644 --- a/src/Hooks/usePill.js +++ b/src/Hooks/usePill.js @@ -11,6 +11,7 @@ import { where, deleteDoc, } from 'firebase/firestore'; +import { strToObjTime, translateTime } from '../utils/transTime'; // 약 추가 함수 / firestore에 약 새로운 약 정보 추가 const addPill = ({ newPill }) => { @@ -90,8 +91,27 @@ export const useGetPillData = (uid) => { * @returns {number} */ const sortByTime = (a, b) => { - if (a.time > b.time) return 1; - if (a.time < b.time) return -1; + /** + * @returns ["AM" or "PM", 시*60 + 분] + */ + const parseTimeOrder = (timeString) => { + // 숫자 비교 + const [selectTime, noon] = translateTime( + strToObjTime(timeString.time), + ).split(' '); + const [hour, minute] = selectTime.split(':'); + return [noon, +hour * 60 + +minute]; + }; + + const [noonA, minuteA] = parseTimeOrder(a); + const [noonB, minuteB] = parseTimeOrder(b); + + // AM PM 비교 + if (noonA > noonB) return 1; + if (noonA < noonB) return -1; + // 분단위 시간 비교 + if (minuteA > minuteB) return 1; + if (minuteA < minuteB) return -1; }; dataArr.sort(sortByTime);