Skip to content

[Feature]: add numeric comparison mode to pipeline sort step #304

@Astro-Han

Description

@Astro-Han

Description

The stepSort function in src/pipeline/steps/transform.ts:58-70 compares values using JavaScript's < / > operators directly. When API responses return numeric values as strings (which is common — e.g. Binance returns "quoteVolume": "1234567.89"), the sort uses lexicographic ordering instead of numeric ordering:

"99" > "1000"  → true  (string comparison: "9" > "1")

This causes ranking commands (top, gainers, losers, etc.) to produce incorrect results for any API that returns numbers as strings.

Current code

// transform.ts:62-68
return [...data].sort((a, b) => {
    const left = isRecord(a) ? a[key] : undefined;
    const right = isRecord(b) ? b[key] : undefined;
    const va = left ?? '';
    const vb = right ?? '';
    const cmp = va < vb ? -1 : va > vb ? 1 : 0;
    return reverse ? -cmp : cmp;
});

Suggested enhancement

Add a numeric: true option to the sort step:

- sort:
    by: quoteVolume
    order: desc
    numeric: true

Implementation: when numeric is true, convert values with Number() before comparison:

const va = numeric ? Number(left) || 0 : (left ?? '');
const vb = numeric ? Number(right) || 0 : (right ?? '');

Alternatively, auto-detect: if both values look numeric (pass isNaN(Number(val)) check), compare as numbers automatically.

Impact

Affects any YAML adapter that sorts on string-encoded numeric fields. Known example: Binance adapter's top, ticker, gainers, losers commands all sort on quoteVolume or priceChangePercent which are returned as strings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions