Skip to content

Conversation

@ExploracuriousAlex
Copy link
Contributor

Problem
When using the --sort-by-column option to sort the portfolio by numeric columns (quantity, price, avgCost, netValue), the values were being sorted alphabetically instead of numerically. This resulted in incorrect ordering, e.g., "10" appearing before "2".

Root Cause
The _get_sort_func() method was returning the raw values without type conversion. Since these values are stored as strings or Decimal objects in string form, Python's sorted() function treated them as strings and performed lexicographic sorting.

Solution
Modified _get_sort_func() to wrap all numeric column values with Decimal() conversion before returning them as sort keys. This ensures proper numeric comparison during sorting.

Changes
Updated sort key lambdas for quantity, price, avgCost, and netValue columns
Changed from lambda x: x["field"] to lambda x: Decimal(x["field"])

Testing
Verified with --sort-by-column quantity that positions are now correctly ordered by numeric value.

@RealCLanger RealCLanger merged commit b76a0ce into pytr-org:master Jan 3, 2026
5 checks passed
@ExploracuriousAlex ExploracuriousAlex deleted the sort_decimals_properly branch January 4, 2026 09:16
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.

2 participants