Hi I’m using the sorted() function in Python to order a bi-dimensionnal array (I want to sort columns just like it can be done in a classic spreadsheet).
In the example below I use itemgetter(0) to sort the grid based on first column’s contents.
But sorted returns empty strings before non-empty ones.
>>> import operator
>>> res = [['charly','male','london'],
... ['bob','male','paris'],
... ['alice','female','rome'],
... ['','unknown','somewhere']]
>>> sorted(res,key=operator.itemgetter(0))
[['', 'unknown', 'somewhere'], ['alice', 'female', 'rome'], ['bob', 'male', 'paris'], ['charly', 'male', 'london']]
>>>
while I would need it to return this:
[['alice', 'female', 'rome'], ['bob', 'male', 'paris'], ['charly', 'male', 'london'], ['', 'unknown', 'somewhere']]
Is there a simple way to do that ?
Use a different key function. One that would work is:
The key is then a tuple with either 0 (False) or 1 (True) in the first position, where True indicates that the first item in the record is blank. The second position has the name field from your original record. Python will then sort first into groups of non-blank and blank names, and then by name within the non-blank name gorup. (Python will also sort by name within the blank name group, but since the name’s blank it’s not going to do anything.)
I also took the liberty of making the name sorting case-insensitive by converting them all to lower-case in the key.
Just replacing the blank names with “ZZZZZZ” or something “alphabetically high” is tempting, but fails the first time some joker puts their name as “ZZZZZZZZ” for a test. I guess something like
'\xff' * 100could work, but it still feels like a hack (also, potential Unicode pitfalls).