1

I have to sort a multi-column file based on two columns. Both the columns have floating point numbers and first sort should from lower to higher and second on higher to lower. Here is sample file:

A        B        C        D
AK       0.01     200.8    NY
DK       0.90     50.5     PHL
AB       0.0002   750.04   Port
GA       0.076    340.00   NY

So, I have to sort on column B first in order low to high and then on column C in order high to low. The code I have is taking a lot of time and make my laptop unresponsive which I believe should not be the case. Moreover, I do not how can I sort column B in 'reverse' i.e. High to low. Here is the code:

fh_in = open(res_file,'r')
res = [line.strip('\n').split('\t') for line in fh_in]##Line converted to list and read
res_list = list(res) ##List to hold results while pre-processing
res_list.sort(key= lambda x: (float(x[int(1)]),-float(x[2])))##Sort on Col A and B
print ('Sorted results:\n',res_list)

How can I sort on both columns with column B sorted from High to low? What would be the fastest way to achieve so, as I have multiple files and each file has 25,000 rows*50 columns?

Would really appreciate your help.

-AK-

1 Answer 1

3

Simply return the negative number:

res_list.sort(key=lambda x: (float(x[1]), -float(x[2])))

This reverses the B sort order, but do note that the data is sorted on column A first.

Note that you can combine the sorting with the previous line in your code by using the sorted() function:

res_list = sorted(res, key=lambda x: (float(x[1]), -float(x[2]))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It worked like a charm. I have edited the answer for future reference - Problem Solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.