numbers = [5, 2, 9, 1, 5, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [1, 2, 5, 5, 6, 9]
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers) # [1, 2, 5, 5, 6, 9]
numbers = [-5, 3, -2, 1, 4]
sorted_numbers = sorted(numbers, key=abs, reverse=True)
print(sorted_numbers) # [-5, 4, 3, -2, 1]
data = ["apple", "banana", "cherry"]
sorted_data = sorted(data, key=len) #Сортируем по длине слов
print(sorted_data) # ['apple', 'cherry', 'banana']
numbers = [111, 42, 9, 33]
sorted_numbers = sorted(numbers, key=lambda x: sum(int(digit) for digit in str(x)))
print(sorted_numbers) # [9, 33, 42, 111]
people = [("Alice", 30), ("Bob", 25), ("Charlie", 35)]
sorted_people = sorted(people, key=lambda person: person[1], reverse=True)
print(sorted_people) # [('Charlie', 35), ('Alice', 30), ('Bob', 25)]
def custom_sort(x):
return -x # Меняем знак, чтобы элементы шли по убыванию
numbers = [5, 1, 3, 2]
sorted_numbers = sorted(numbers, key=custom_sort)
print(sorted_numbers) # Вывод: [5, 3, 2, 1]
text = "python"
sorted_text = sorted(text)
print(''.join(sorted_text)) # "hnopty"
mixed_text = "Python"
print(''.join(sorted(mixed_text))) # Phnoty
data = {3, 1, 4, 1, 5, 9}
sorted_data = sorted(data)
print(sorted_data) # [1, 3, 4, 5, 9]
data = {"c": 3, "a": 1, "b": 2}
sorted_keys = sorted(data)
print(sorted_keys) # ['a', 'b', 'c']
sorted_dict = {key: data[key] for key in sorted(data)}
print(sorted_dict) # {'a': 1, 'b': 2, 'c': 3}
data = {"c": 3, "a": 1, "b": 2}
sorted_by_values = {key: value for key, value in sorted(data.items(), key=lambda item: item[1])}
print(sorted_by_values) # {'a': 1, 'b': 2, 'c': 3}
sorted_by_values_desc = {key: value for key, value in sorted(data.items(), key=lambda item: item[1], reverse=True)}
print(sorted_by_values_desc) # {'c': 3, 'b': 2, 'a': 1}
data = (5, 2, 9, 1, 7)
sorted_tuple = tuple(sorted(data))
print(sorted_tuple) # (1, 2, 5, 7, 9)
data = [(3, 'яблоко'), (1, 'банан'), (2, 'груша')]
sorted_data = sorted(data)
print(sorted_data) # [(1, 'банан'), (2, 'груша'), (3, 'яблоко')]
sorted_by_second = sorted(data, key=lambda x: x[1])
print(sorted_by_second) # [(1, 'банан'), (3, 'яблоко'), (2, 'груша')]
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
students = [Student("Alice", 90), Student("Bob", 80), Student("Eve", 95)]
students.sort(key=lambda student: student.grade)
for student in students:
print(student.name, student.grade)
Bob 80
Alice 90
Eve 95
numbers = [8, 3, 7, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [1, 3, 5, 7, 8]
print(numbers) # [8, 3, 7, 1, 5] — оригинал не изменился
mixed = ["2", 1, "3", 4]
sorted_mixed = sorted(mixed, key=int) # Все элементы приводятся к числу
print(sorted_mixed) # [1, "2", "3", 4]
numbers = [5, 1, 3, 2]
numbers.sort(reverse=True) # Сортируем по убыванию
print(numbers) # [5, 3, 2, 1]
students = [("Alice", 90), ("Bob", 85), ("Charlie", 90)]
sorted_students = sorted(students, key=lambda x: x[1])
[("Bob", 85), ("Alice", 90), ("Charlie", 90)]
students = [("Charlie", 90), ("Alice", 85), ("Bob", 85)]
students_by_name = sorted(students, key=lambda x: x[0]) # По имени
final_sort = sorted(students_by_name, key=lambda x: x[1]) # По оценке
[("Alice", 85), ("Bob", 85), ("Charlie", 90)]