This is will not answer your question but it might help you to solve your problem.
Here is how to sort lists in python:
import pprint
table = [
[1,'H',-1],
[2,'D',2],
[3,'D',8],
[4,'D',1],
[5,'H',68],
[6,'H',97],
[7,'H',88],
]
print 'Before sorting:'
print '[OBJECTID, NameID, Number]'
pprint.pprint(table)
print
print 'After sorting:'
print '[OBJECTID, NameID, Number]'
pprint.pprint(
sorted(table, key=lambda x: (x[1], x[2]))
)
Excetuting this script gives:
Before sorting:
[OBJECTID, NameID, Number]
[[1, 'H', -1],
[2, 'D', 2],
[3, 'D', 8],
[4, 'D', 1],
[5, 'H', 68],
[6, 'H', 97],
[7, 'H', 88]]
After sorting:
[OBJECTID, NameID, Number]
[[4, 'D', 1],
[2, 'D', 2],
[3, 'D', 8],
[1, 'H', -1],
[5, 'H', 68],
[7, 'H', 88],
[6, 'H', 97]]