0

I am trying to sort a list having key value pair on the basis of values. But as per the current code(added below) everytime it is getting sorted in terms of the keys (in alphabetical manner). The data is provided in a file from where they are readed. Please find below the data: Vikram - 980 Super - 300 Slipsheet - 125 Queen - 173

Can you please help me in getting the values sorted as per values in the key value pair in the list.

n = int(input("Enter the n value \n"))
answer = []
a = []
with open('file_in.txt') as r:
    for lines in r:
        answer.append(lines)
    answer.sort(reverse = True)
    #print(answer)
for i in range(n):
    a.append(answer[i])
print(sorted(a))

Currently the output is displayed as:

['Queen - 173', 'Slipsheet - 125\n', 'Super - 300\n', 'Vikram - 980\n']

But the output should be:

['Vikram - 980','Super - 300','Queen - 173','Slipsheet - 125']
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
luciferxxx
  • 59
  • 1
  • 8

0 Answers0