0

I'm a newbie . I have a problem .

That problem is how to sort a list objects in Python similar struct in C

Example: I have code:

class struct():

    def __init__(self,first=0,second=0):
        self.f=first
        self.s=second

for i in range(1,n+1):
     a[i]=struct()
     a[i].f,a[i].s=map(int,input().split())


a.sort(key=???)
for i in range(1,n+1):
    print(a[i].f," ",a[i].s)

and the test :

4 3
7 4
15 10
2 2
1 5

I want to sort the list a [i] .f in ascending order and if there are more a [i] .f , I want to sort the a [i] .s in descending order.

Please tell me the solution. Thank u for reading

1

1 Answer 1

1

See the functools library, total_ordering class. Apply that as a decorator to your class and define the logic for which of two objects sorts higher / lower.

Sign up to request clarification or add additional context in comments.

Comments

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.