uri-mog / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

Adding enhanced capabilty of sorting to pretty table #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi, 

I use the pretty table and frequently find myself where I would like to sort 
the table using a key function, currently I cannot do it inside prettytable, 
and I have have to either construct it after all my sorting or deconstruct, 
sort and reconstruct the table. 

Would it be possible to add this feature? If you want I can take a crack at it 
and submit a patch. 

Thanks
Aman

Original issue reported on code.google.com by amannijh...@gmail.com on 6 Jan 2012 at 4:20

GoogleCodeExporter commented 8 years ago
Also sorry I should have made it an enhancement request instead of it being an 
issue

Original comment by amannijh...@gmail.com on 6 Jan 2012 at 4:21

GoogleCodeExporter commented 8 years ago
Hi there,

Thanks for this suggestion.  Sorry for my late response.

Do you mean you just want to be able to specify a function that is then passed 
to list.sort with the key= parameter?  If so, I think that should be pretty 
easy and I'll have a go at implementing it soon.

Cheers,
Luke

Original comment by luke@maurits.id.au on 23 Mar 2012 at 3:52

GoogleCodeExporter commented 8 years ago
Okay, I have now committed a change to trunk which allows you to do:

MyTable.sort_key = foo

to use an arbitrary sort key function.  I would appreciate it if you could give 
this a test and let me know how it works out in your particular use case.  
Please note that if you are currently using PrettyTable 0.5, there has been an 
API change between that version and the version currently in trunk.  Read the 
Wiki article at http://code.google.com/p/prettytable/wiki/Tutorial06 for an 
overview of the changes involved.

Also, be aware that the .sort_key setting will interact with the .sortby 
setting.  Firstly, PrettyTable will do no sorting at all unless .sortby is set 
to a valid fieldname.  Also, note that the sort key function will be passed 
lists, with the following format:

[sortby_col, col1, col2, col3,...]

Each list corresponds to one row of the table.  The first element in the list 
is that row's value for the column which is being sorted by, and the subsequent 
values are the values for each column, in order, *including a repetition of the 
column being sorted by*.  This is because of the way that PrettyTable uses the 
Decorte-Sort-Undecorate paradim internally.  It's possible that I'll rewrite 
this in future to be a bit cleaner, but for now you'll just have to be careful!

Original comment by luke@maurits.id.au on 24 Mar 2012 at 7:26

GoogleCodeExporter commented 8 years ago
Given lack of negative feedback about this change for over one month, I am now 
closing the issue as "Fixed".  If the current implementation of sort key 
function setting turns out to have shortcomings, people should feel free to 
submit a new Issue.

Original comment by luke@maurits.id.au on 30 Apr 2012 at 7:03