how to sort list in python without sort function code example
Example 1: how do i sort list in python
my_list = [9, 3, 1, 5, 88, 22, 99]
my_list = sorted(my_list, reverse=True)
print(my_list)
my_list = sorted(my_list, reverse=False)
print(my_list)
my_list.sort(reverse=True)
print(my_list)
print(my_list[::-1])
Example 2: order a list without sort
n = int(input("Elementos da lista = "))
lista = []
for i in range(n):
x = int(input("Valor (0 a 9) = "))
if (i == 0) or (x > lista[- 1]):
lista.append(x)
else:
pos = 0
while (pos < len(lista)):
if x <= lista[pos]:
lista.insert(pos , x)
break
pos = pos + 1
Example 3: how to manually sort a list in python
Numbers = []
iterate = 0
while len(Numbers)<5:
try:
x = int(input("Insert the number you want in the list: "))
Numbers.append(x)
except:
print("The input MUST be a number.")
continue
for iteration_count in range(len(Numbers)):
for j in range(0,len(Numbers)-1):
if (Numbers[j]>Numbers[j+1]):
Numbers[j],Numbers[j+1] = Numbers[j+1],Numbers[j]
print(f"Here are the sorted numbers:{Numbers}")
Example 4: sorting numbers in python without sort function
number=[1,5,6,9,0]
for i in range(len(number)):
for j in range(i+1,len(number)):
if number[i]<number[j]:
number[i],number[j]=number[j],number[i]
print(number)