In python, sorting on date field, field may sometimes be null -


Coming to me in a real way to deal with this way. The data is back from reading the database I have. Although I want to sort on accoutingdate, sometimes it can be fake. I am currently working the following:

  result = sorted (result, key = operator.atmeter ('accountingdate'), reverse = true)  

But, because some details are empty, "this bomb with typeError: can not compare datetime.date for any type".

What is the "most accurate" or "most powerful" way to handle this? It's definitely okay to use the key = function, you have to decide how do you want to treat any values ​​- a Select Date Time Values ​​that you want to be considered equivalent to none for sorting purposes. Eg:

  import routine brain = datetime.date (datetime.MINYEAR, 1, 1) def getaccountingdate (x): return x ['accountingdate'] or brain result = sort (result, key = Getaccountingdate, reverse = true)  

Just see how simple it is by defining a cmp function - and if you do some benchmarking, then you It's also quite fast! There is no reverse in using the cmp function instead of the key function, and it would be a bad design to do this.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -