Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python custom sort

# Works with python 3+
def compare(item):
    #First index (index = 1) used for comparison first -> bigger to smaller -> Descending
    #Second index (index = 2) used for comparison after that -> smaller to bigger -> Ascending
    return (-item[1], item[2])

output = [['perfect', 3, 2], ['just', 1, 10], ['get', 1, 6], ['makes', 1, 1]]
output.sort(key=compare)

>> [['perfect', 3, 2], ['makes', 1, 1], ['get', 1, 6], ['just', 1, 10]]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #custom #sort
ADD COMMENT
Topic
Name
4+6 =