-1

Example:

def Sort(sub_li): 
    l = len(sub_li) 
    for i in range(0, l): 
        for j in range(0, l-i-1): 
            if (sub_li[j][1] > sub_li[j + 1][1]): 
                tempo = sub_li[j] 
                sub_li[j]= sub_li[j + 1] 
                sub_li[j + 1]= tempo 
    return sub_li 
  
# Driver Code 
sub_li =[['rishav', 10], ['akash', 5], ['ram', 20], ['gaurav', 15]] 
print(Sort(sub_li)) 

My code and list of lists is Listy, which I want to sort by sum:

suma += Highest_score
                lists.append([Highest_score, i])
            lists.extend([suma])
            listy.append(lists)


for index in listy:

    for i in range(0, len(index)): 
  
        if i == (len(index)-1): 
 
            print(listy, I)  

So I have list of list consist of

[[0.352, 'J'], [0.36, 'J'], [0.627, '7'], [0.412, '8'], [0.258, '8'], [0.317, 'R'], [0.383, 'K'], 2.709]
[[0.348, 'J'], [0.389, 'J'], [0.499, '7'], [0.369, '8'], [0.365, '8'], [0.289, 'R'], [0.342, 'K'], 2.601]
[[0.384, 'J'], [0.387, 'J'], [0.282, '7'], [0.239, '8'], [0.25, '8'], [0.256, 'R'], [0.357, 'K'], 2.155]
[[0.379, 'J'], [0.391, 'J'], [0.458, '7'], [0.235, '8'], [0.289, '8'], [0.275, 'R'], [0.378, 'K'], 2.4050000000000002]
[[0.401, 'J'], [0.462, '7'], [0.422, 'J'], [0.287, '8'], [0.271, '8'], [0.286, 'R'], [0.377, 'K'], 2.5060000000000002]
[[0.35, 'J'], [0.382, 'J'], [0.517, '7'], [0.36, '8'], [0.259, '8'], [0.331, 'R'], [0.36, 'K'], 2.5589999999999997]
[[0.366, 'J'], [0.391, 'J'], [0.299, '7'], [0.234, '8'], [0.269, '8'], [0.239, 'R'], [0.3, 'K'], 2.098]

Each one has last element which is float, I want to sort my list of lists "Listy" which consist of "lists", every lists have suma like last element.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kat Misik
  • 19
  • 7
  • https://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list/4174955 – AL.Sharie Mar 08 '21 at 09:26

1 Answers1

0

def Sort(listy):

l = len(listy)
for i in range(0, l):

    for j in range(0, l-i-1):
        if (listy[j][-1] > listy[j+ 1][-1]):
            temp = listy[j]
            listy[j]= listy[j + 1]
            listy[j + 1]= temp
return listy

print(Sort(listy))

Kat Misik
  • 19
  • 7