알림목록 종목별 정렬시 시간별로 정렬되도록 수정
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import type { PaperTradeDto } from './backendApi';
|
||||
import type { TradeNotificationItem } from '../contexts/TradeNotificationContext';
|
||||
import type { TradeNotificationListSort } from './tradeNotificationListLayout';
|
||||
|
||||
/** 종목(마켓) 코드 비교 — 한글 정렬 */
|
||||
export function compareMarketSymbol(a: string, b: string): number {
|
||||
return a.localeCompare(b, 'ko');
|
||||
}
|
||||
|
||||
/** epoch ms·ISO 문자열·초 단위 타임스탬프 → 오름차순(이전 → 최근) */
|
||||
export function compareTimeAsc(
|
||||
a: number | string | null | undefined,
|
||||
b: number | string | null | undefined,
|
||||
): number {
|
||||
return toTimeMs(a) - toTimeMs(b);
|
||||
}
|
||||
|
||||
function toTimeMs(t: number | string | null | undefined): number {
|
||||
if (t == null) return 0;
|
||||
if (typeof t === 'number') {
|
||||
if (!Number.isFinite(t)) return 0;
|
||||
return t < 1e12 ? t * 1000 : t;
|
||||
}
|
||||
const d = new Date(t);
|
||||
return Number.isNaN(d.getTime()) ? 0 : d.getTime();
|
||||
}
|
||||
|
||||
/** 종목순 → 동일 종목은 체결·수신 시각 오름차순 (매수·매도 흐름) */
|
||||
export function sortByMarketThenTimeAsc<T>(
|
||||
items: readonly T[],
|
||||
getMarket: (item: T) => string,
|
||||
getTime: (item: T) => number | string | null | undefined,
|
||||
): T[] {
|
||||
return [...items].sort((a, b) => {
|
||||
const byMarket = compareMarketSymbol(getMarket(a), getMarket(b));
|
||||
if (byMarket !== 0) return byMarket;
|
||||
return compareTimeAsc(getTime(a), getTime(b));
|
||||
});
|
||||
}
|
||||
|
||||
export function sortPaperTradesForDisplay(trades: readonly PaperTradeDto[]): PaperTradeDto[] {
|
||||
return sortByMarketThenTimeAsc(trades, t => t.symbol, t => t.createdAt);
|
||||
}
|
||||
|
||||
export function sortTradeNotifications(
|
||||
items: TradeNotificationItem[],
|
||||
sort: TradeNotificationListSort,
|
||||
): TradeNotificationItem[] {
|
||||
const arr = [...items];
|
||||
if (sort === 'market') {
|
||||
return sortByMarketThenTimeAsc(arr, n => n.market, n => n.receivedAt);
|
||||
}
|
||||
if (sort === 'oldest') {
|
||||
return arr.sort((a, b) => compareTimeAsc(a.receivedAt, b.receivedAt));
|
||||
}
|
||||
return arr.sort((a, b) => compareTimeAsc(b.receivedAt, a.receivedAt));
|
||||
}
|
||||
Reference in New Issue
Block a user