I'm running Python 2.7 and I've got an ObjectListView
displaying loads of demographic info. I can get it to sort correctly, but the output is displayed in 100000.0
format. When I convert the integer to a string using the locale module, it sorts the descending strings like 9,181, 9,069, 818, 813, 8,730,
etc. Any ideas how to sort like integers, but display the output as comma formatted in the ObjectListView
?
Asked
Active
Viewed 588 times
2

Cœur
- 37,241
- 25
- 195
- 267

Tim Gottgetreu
- 483
- 1
- 8
- 21
-
Sort the data before the formatting. If `ObjectListView` is like most datagrid components (i.e. kind of terrible at non-RAD use cases), you'll have to implement custom sorting for that column. – millimoose Oct 16 '12 at 00:05
-
From skimming the `ObjectListView` documentation, it seems like it should sort based on the underlying model value. Can you show some example of how you're wiring this up and where you're converting the number to a string? (Make it a simple example if possible, like with a dummy class that only has one integer column.) – millimoose Oct 16 '12 at 00:10
2 Answers
1
You can order yourColumn
by their integer values rather than their display string using:
yourColumn = ColumnDefn("Title", "center", 100, "title", stringConverter=int_to_string_with_commas)
-- see smarter string conversions.
.
Where int_to_string_with_commas
is a function converting integers to strings (with commas):
import locale
locale.setlocale(locale.LC_ALL, 'en_US') # your locale here
def int_to_string_with_commas(value):
return locale.format("%d", value, grouping=True)
For other ways to do write int_to_string_with_commas
see this question.
.

Community
- 1
- 1

Andy Hayden
- 359,921
- 101
- 625
- 535
-
Your formatting function is kind of worse than the OP's approach. Using `locale` to format this the way that's set up on the user's system is the way to go - hardcoding the format would go beside the point of this. (That said, the way the column is defined might be helpful. Can't really say without the OP providing code.) – millimoose Oct 16 '12 at 00:39
-
@millimoose However you code, [int_to_string_with_commas](http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators-in-python-2-x) the `yourColumn` line will be the same. The point is, it will order like value, rather than `int_to_string_with_commas(value)`. – Andy Hayden Oct 16 '12 at 00:42
-
I understand what you're getting at, it's just that it's mildly confusing to suggest an inferior implementation of the function than the one the OP already says he has done correctly, even if it's tangential to the actual question. (For that matter, so is spending half your answer on a tangential concern.) I'd just use an obviously dummy or vague implementation, since it's not important how the string is being formatted. – millimoose Oct 16 '12 at 01:19
-
@millimoose Rearranged to add the more important section first (and used locale.) – Andy Hayden Oct 16 '12 at 08:40
0
Thanks for putting me on the right track! In the end I just ended up adding two lines to the ObjectListView.py source. Around line 3601, I changed the _StringToValue Function to include:
if converter == "%cd": ##New Line
return locale.format("%d", value, grouping=True) ##New Line
else:
fmt = converter or "%s"
try:
return fmt % value
except UnicodeError:
return unicode(fmt) % value
also needed to set locale after the local import, as show above:
locale.setlocale(locale.LC_ALL, 'en_US')
Thank you, I've been pounding my head on this one for a while now. Cheers!

Tim Gottgetreu
- 483
- 1
- 8
- 21