SoFunction
Updated on 2025-03-05

Implementation of specific scenarios of Python sequence sorting

In python, when it comes to list sorting, the built-in sort() method or the global sorted() method is used, the following differences are as follows:

1. The sort() method can only be used for list sorting, not for other iterable sequences such as strings, dictionaries, etc.; the sorted() method can be used for all iterable sequences;

2. The sort() method is to sort on the original list and return None, which will destroy the original list structure; the sorted() method is to return a new sequence after sorting, which has no effect on the original list;

#sort() sort>>> a=[6,9,8,4,3,1,2]
>>> b=()
>>> print(b)
None
>>> print(a)
[1, 2, 3, 4, 6, 8, 9]

#sorted() sort>>> a=[6,9,8,4,3,1,2]
>>> b=sorted(a)
>>> print(b)
[1, 2, 3, 4, 6, 8, 9]
>>> print(a)
[6, 9, 8, 4, 3, 1, 2]

When sorting dictionary, the sorted() method is sorted by the dictionary key (key), as follows:

>>> a={5:'A',1:'E',4:'B',2:'D',3:'C'}
>>> b=sorted(a)
>>> print(b)
[1, 2, 3, 4, 5]

If you need to sort by the value of the dictionary, you can use the following method:

>>> a={5:'A',1:'E',4:'B',2:'D',3:'C'}
>>> b=sorted((), key=lambda item:item[1])
>>> print(b)
[(5, 'A'), (4, 'B'), (3, 'C'), (2, 'D'), (1, 'E')]

Advanced Usage

Both the sort() method and the sorted() method can specify parameters to handle the sorting of some complex scenarios.

1. Key parameter: Specify a function, which can be a built-in function or a function defined by yourself. This function will be called before each element is compared.

2. reverse parameter: This parameter specifies True or False to perform descending or ascending order, and the default is False (ascending order).

as follows:

a = ["This", "A", "is", "bag"]
b = sorted(a, key=)
c = sorted(a, key=, reverse=True)
print(b)
print(c)

['A', 'bag', 'is', 'This']
['This', 'is', 'bag', 'A']

A wider usage is to sort the sequence of complex objects with certain values ​​of complex objects, such as:

A list holds the name, grade and score of each student

#No one answers the questions encountered during study?  The editor has created a Python learning exchange group: 531509025student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96)
]

Scene 1. Sort by grade from high to low

student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96)
]
print(sorted(student_tuples, key=lambda student: student[1]))

[('john', 'A', 96), ('andy', 'A', 92), ('cany', 'A', 96), ('jane', 'B', 82), ('dave', 'B', 85), ('leky', 'D', 63)]

Scene 2. Sort from high to low by score

Method (1), use reverse parameter

student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96),
]
print(sorted(student_tuples, key=lambda student: student[2], reverse=True))

[('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

Method (2), use a negative sign (-)

student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96),
]
print(sorted(student_tuples, key=lambda student: -student[2]))

[('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

Note: The minus sign (-) can only be used before a number, not before a string.

Scene 3. Sort by grade from high to low, and the same grade is sorted by score from high to bottom.

student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96)
]
print(sorted(student_tuples, key=lambda student: [student[1], -student[2]]))

[('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

Scene 4. Sort by grade from low to high. If grade is the same, sort by score from low to high.

student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96)
]
print(sorted(student_tuples, key=lambda student: [student[1], -student[2]],reverse=True))

[('leky', 'D', 63), ('jane', 'B', 82), ('dave', 'B', 85), ('andy', 'A', 92), ('john', 'A', 96), ('cany', 'A', 96)]

Scene 5. Sort by grade from low to high. If grades are the same, sort from low to high. Finally, ascending order by name

The name is a string, and it cannot be sorted with "symbol (-)" before the string. You can rewrite the "rich comparison" method.

#No one answers the questions encountered during study?  The editor has created a Python learning exchange group: 531509025class Reversinator(object):
    def __init__(self, obj):
         = obj

    def __lt__(self, other):
        return  < 


student_tuples = [
    ('john', 'A', 96),
    ('leky', 'D', 63),
    ('andy', 'A', 92),
    ('jane', 'B', 82),
    ('dave', 'B', 85),
    ('cany', 'A', 96),
]

print(sorted(student_tuples, key=lambda student: [student[1], -student[2], Reversinator(student[0])], reverse=True))

[('leky', 'D', 63), ('jane', 'B', 82), ('dave', 'B', 85), ('andy', 'A', 92), ('cany', 'A', 96), ('john', 'A', 96)]

This is the end of this article about the implementation of specific scenarios of Python sequence sorting. For more related Python sequence sorting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!