42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import type { TradeHistoryRow } from './backtestEquity';
|
|
|
|
export type TradeHistorySortKey = 'time' | 'side' | 'price' | 'pnlPct';
|
|
export type TradeHistorySortDir = 'asc' | 'desc';
|
|
|
|
export function sortTradeHistoryRows(
|
|
rows: readonly TradeHistoryRow[],
|
|
key: TradeHistorySortKey,
|
|
dir: TradeHistorySortDir,
|
|
): TradeHistoryRow[] {
|
|
const mul = dir === 'asc' ? 1 : -1;
|
|
return [...rows].sort((a, b) => {
|
|
let cmp = 0;
|
|
switch (key) {
|
|
case 'time':
|
|
cmp = a.time - b.time;
|
|
break;
|
|
case 'side':
|
|
cmp = a.side.localeCompare(b.side);
|
|
break;
|
|
case 'price':
|
|
cmp = a.price - b.price;
|
|
break;
|
|
case 'pnlPct':
|
|
cmp = compareNullableNumber(a.pnlPct, b.pnlPct);
|
|
break;
|
|
default:
|
|
cmp = 0;
|
|
}
|
|
if (cmp !== 0) return cmp * mul;
|
|
return a.id - b.id;
|
|
});
|
|
}
|
|
|
|
/** 손익 없음(매수)은 항상 목록 끝 */
|
|
function compareNullableNumber(a: number | undefined, b: number | undefined): number {
|
|
if (a == null && b == null) return 0;
|
|
if (a == null) return 1;
|
|
if (b == null) return -1;
|
|
return a - b;
|
|
}
|