2

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

3
  • Why are you using a custom sort(looks like insertion sort)? Lists in Python have a method .sort() to sort them in-place. Commented Oct 7, 2014 at 22:38
  • What do you want to sort on, aka what is the key in Merchant_9983 47463 is it "Merchant_9983" the number 9983 or 47463? Commented Oct 7, 2014 at 22:39
  • I think 1 month qualifies as just plain new. ;) Welcome to StackOverflow, and we all thank you for trying before you posted here. Commented Oct 7, 2014 at 22:58

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

'Merchant_2' > 'Merchant_100' --> True, so your second code snippet won't work perfectly. Also since you've already read everything in memory then using data.sort() will be a better option, or call sorted() on the file object.
@AshwiniChaudhary: for non-default sort e.g. by numeric value, pass a comparison fn: sorted(... cmp=mycmpfn )
@smci cmp is deprecated(removed in Python 3), and is slow. Use key.
@AshwiniChaudhary key=lambda i: int(i.split()[1].replace('Merchant_', '')) Actually, I'd be inclined to create an object or dictionary for these values, so they could be named, in favor of the split. Would make things more readable than the split does, although the line would have to be parsed, of course.
@jpmc26 You mean i.split()[0].replace(...? There's no 'Merchant_' at index 1 after splitting.
|
1

Like Ashwini said, just use sort() method, or better still, sorted() function like Reymond Hettinger says. Here we rely on the default sort order, which will use simple alphanumeric order of the string, hence order by field1 first, then field 2...

So you can simply do:

my_sorted_contents = sorted(line.strip() for line in open('MYTEXTFILE'))
my_sorted_contents
['Merchant_9976 20122', 'Merchant_9977 91840', 'Merchant_9978 92739', 'Merchant_9979 97252', 'Merchant_9980 76885', 'Merchant_9981 67835', 'Merchant_9982 42201', 'Merchant_9983 47463']

If you actually want to split lines into fields (such that you could use a more general field-based sort order), then throw in a .split() call before you sort.

For non-default sort orders e.g. by numeric value ("Merchant_100" > "Merchant2"), pass a comparison key, as per jpmc26's comment: sorted(... key=mykeyn )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.