Skip to content

Commit

Permalink
refactor: 시간순 정렬 #1 #2 #5 #14
Browse files Browse the repository at this point in the history
AM PM 구분하고 정렬합니다. 그 다음 시간에 따라 순서를 정렬합니다.
  • Loading branch information
arch-spatula committed Jan 12, 2023
1 parent 6a2efc7 commit 2bfe7ec
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Hooks/usePill.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
where,
deleteDoc,
} from 'firebase/firestore';
import { strToObjTime, translateTime } from '../utils/transTime';

// 약 추가 함수 / firestore에 약 새로운 약 정보 추가
const addPill = ({ newPill }) => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2bfe7ec

Please sign in to comment.