Fix: Incorrect sorting of numeric columns in portfolio #298
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using the
--sort-by-columnoption 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'ssorted()function treated them as strings and performed lexicographic sorting.Solution
Modified
_get_sort_func()to wrap all numeric column values withDecimal()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"]tolambda x: Decimal(x["field"])Testing
Verified with --sort-by-column quantity that positions are now correctly ordered by numeric value.