Python sorting a text file?
Question: So I know how to import a texfile and sort numbers such as:

    
    
    1
    6
    4
    6
    9
    3
    5
    

But I don't know how to sort a data that looks like:

    
    
    Merchant_9976 20122
    Merchant_9977 91840
    Merchant_9978 92739
    Merchant_9979 97252
    Merchant_9980 76885
    Merchant_9981 67835
    Merchant_9982 42201
    Merchant_9983 47463
    

Here's my code so far

    
    
    def sort_slow(seq):
        for i in range(1, len(seq)):
            j = i
            while j > 0 and seq[j - 1] > seq[j]:
                seq[j - 1], seq[j] = seq[j], seq[j - 1]
                j -= 1
        return seq
    
    
    def main():
        fileName = str(input('Please enter a filename: '))
        file = open(fileName)
        sort1 = []
        for lines in file:
            sort1.append(int(lines.strip()))
        sort_slow(sort1)
        print(sort1)
    
    main()
    

I'm relatively new to coding.*about 1 month


Answer: The following will sort by the integer in the right column

    
    
    with open('file.txt', 'r') as f:
        data = f.readlines()
        sorted_data = sorted(data, key=lambda i: int(i.split()[1]))
        print(sorted_data)
    

Or if you simply want them sorted by the merchant number

    
    
    with open('file.txt', 'r') as f:
        data = f.readlines()
        sorted_data = sorted(data)
        print(sorted_data)
    

