fix(ccusage): sort session reports by cost instead of last activity by bluzername · Pull Request #907 · ccusage/ccusage · GitHub
Skip to content

fix(ccusage): sort session reports by cost instead of last activity#907

Merged
ryoppippi merged 2 commits into
ccusage:mainfrom
bluzername:fix/session-sort-by-cost
May 11, 2026
Merged

fix(ccusage): sort session reports by cost instead of last activity#907
ryoppippi merged 2 commits into
ccusage:mainfrom
bluzername:fix/session-sort-by-cost

Conversation

@bluzername

@bluzername bluzername commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Problem

The Session Reports documentation says:

Sessions are sorted by cost (highest first) by default

But actually running pnpx ccusage session shows entries sorted by Last Activity timestamp instead of by Cost. This was confusing because the expensive sessions were not at the top like I was expecting.

What I changed

In apps/ccusage/src/data-loader.ts, the loadSessionData function was using sortByDate on lastActivity field:

// before
return sortByDate(sessionFiltered, (item) => item.lastActivity, options?.order);

Changed to sort by totalCost using fast-sort directly:

// after
const sorted = sort(sessionFiltered);
const order = options?.order ?? 'desc';
switch (order) {
  case 'asc':
    return sorted.asc((item) => item.totalCost);
  case 'desc':
    return sorted.desc((item) => item.totalCost);
  default:
    unreachable(order);
}

Also updated:

  • The JSDoc comment that said "sorted by last activity" to say "sorted by cost"
  • The 3 related tests to use different costUSD values per session so they actually verify sorting by cost and not just any order

Testing

  • All 252 tests pass (127 in data-loader.ts)
  • pnpm run format passes
  • Tests now use costUSD: 0.01, 0.05, 0.10 to verify cost ordering works correct

Closes #903

Summary by CodeRabbit

  • Improvements
    • Sessions are now sorted by total cost (highest to lowest) by default, making it easier to spot expensive sessions.
    • Sorting can be reversed to show lowest-cost sessions first.
    • Display-mode session lists now align ordering with the displayed cost values for consistent viewing.

Review Change Stack

@coderabbitai

coderabbitai Bot commented Mar 25, 2026

Copy link
Copy Markdown

@ryoppippi ryoppippi merged commit 9f31ebc into ccusage:main May 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session reports use Last Activity for sort order instead of costs

2 participants