I have a text file containing the following data.

ascon1 201707011 John 77.5 11.5 11.5 11.5
ascon1 201707012 Grld 70.0 11.5 11.5 11.5
ascon1 201707013 Josh 79.5 11.5 11.5 11.5 
ascon1 201707014 Jess 67.5 11.5 11.5 11.5 
ascon1 201707015 Jack 97.5 11.5 11.5 11.5 

I need the data to look like this.

ascon1 201707015 Jack 97.5 11.5 11.5 11.5 
ascon1 201707013 Josh 79.5 11.5 11.5 11.5
ascon1 201707011 John 77.5 11.5 11.5 11.5 
ascon1 201707012 Grld 70.0 11.5 11.5 11.5 
ascon1 201707014 Jess 67.5 11.5 11.5 11.5 

so here is my code. The above part is working using the inbuilt sorted function from python. but in ascending order instead of decending order.

with open('input.txt') as f:
    lines = [line.split(' ') for line in f]
output = open("input(sorted).txt", 'w')

for line in sorted(lines, key=itemgetter(3)):
    output.write(' '.join(line))

output.close()

I want to use the quick-sort function to solve this problem. this is my function.

def qsort(d_val):
    if len(d_val) <= 1: return d_val
    return qsort([lt for lt in d_val[1:] if lt < d_val[0]]) + d_val[0:1] + \
           qsort([ge for ge in d_val[1:] if ge >= d_val[0]])

The sorted() function has a boolean reverse parameter that allows to sort in descending order

sorted(lines, key=itemgetter(3), reverse=True)

This is much more efficient than trying to write your own quicksort method.

but i need to use my own fuction. it is a requirement.

i will keep the reverse parameter in mind for future use.

You can use > and <= in the qsort() function to sort in descending order. Is there a question with this function? You could add a key argument like so

def qsort(d_val, key=lambda x: x):
    if len(d_val) <= 1: return d_val
    return qsort([lt for lt in d_val[1:] if key(lt) > key(d_val[0]])) + d_val[0:1] + \
           qsort([ge for ge in d_val[1:] if key(ge) <= key(d_val[0]]))

There are other implementations, such as inplace implementations. One could also add a reverse parameter.

when i run the code it is giving me an error.

return qsort([lt for lt in d_val[1:] if key(lt) > key(d_val[0]])) + d_val[0:1] + \
                                                                                                                                                ^

SyntaxError: invalid syntax

the square bracket in key(d_val[0]]))

It is key(d_val[0])]) . SyntaxError always has a very simple solution.

thanks for the help.

This problem was solved like 40 years ago.

$ sort -t" " -rnk4 data.tsv
ascon1 201707015 Jack 97.5 11.5 11.5 11.5
ascon1 201707013 Josh 79.5 11.5 11.5 11.5
ascon1 201707011 John 77.5 11.5 11.5 11.5
ascon1 201707012 Grld 70.0 11.5 11.5 11.5
ascon1 201707014 Jess 67.5 11.5 11.5 11.5
  • -t" " to tell sort to split on spaces
  • -r for reverse (highest first)
  • -n to tell sort that it's a numeric field
  • -k4 to tell sort that we're loking at the fourth column

@pty The idea was to do the same in python.

Ah ok. The original post didn't sound like a typical "do my homework" request.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.