Jay
0
Q:

sort an array python

my_list = [9, 3, 1, 5, 88, 22, 99]

# sort in decreasing order
my_list = sorted(my_list, reverse=True) 
print(my_list)

# sort in increasing order
my_list = sorted(my_list, reverse=False) 
print(my_list)

# another way to sort using built-in methods
my_list.sort(reverse=True)  
print(my_list)

# sort again using slice indexes
print(my_list[::-1])

# Output
# [99, 88, 22, 9, 5, 3, 1]
# [1, 3, 5, 9, 22, 88, 99]
# [99, 88, 22, 9, 5, 3, 1]
# [1, 3, 5, 9, 22, 88, 99]
6
numbers = [1, 3, 4, 2] 
  
# Sorting list of Integers in ascending in place
numbers.sort() 
  
print(numbers) 
6
a = [1, 2, 0, 8, 4, 5, 3, 7, 6]
print(a.sort())
1

a = (1, 11, 2)
x = sorted(a)
print(x)
 
4

    cars = ['Ford', 'BMW', 'Volvo']


    cars.sort(reverse=True) 
1
arr = [1,4,2,7,5,6]
#sort in ascending order
arr = arr.sort() 

#sort in descending order
arr = arr.sort(reverse = True)
1
l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
1
List_name.sort()
This will sort the given list in ascending order.
1
old_list = [3,2,1]
old_list.sort()
1
x = [2, 8, 1, 4, 6, 3, 7]  
  
print ("Sorted List returned :"),  
print (sorted(x))  
  
print ("\nReverse sort :"),  
print (sorted(x, reverse = True))
0

New to Communities?

Join the community