0

A list of Point class objects is given. The Point class is as follows:

class Point:
    def __init__(self, value, segment, is_start=False):
        self.value = value
        self.is_start = is_start

The goal is to sort the list by the value attribute provided that if two objects have the same value attributes, the object whose is_start value is True should go before the object whose is_start value is False.

An example of input:

[Point(3, False), Point(3, True), Point(1, True),]

I've tried using array.sort(key=lambda x: x.value) and the output was:

[Point(1, True), Point(3, False), Point(3, True),]

But it doesn't satisfy the condition stated before (Point(3, True) should be before Point(3, False)).

An example of desired output:

[Point(1, True), Point(3, True), Point(3, False),]

What function to use to satisfy the stated condition?

1 Answer 1

1

Try this.

sorted(x, key=lambda x: (x.value, not x.is_start))
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.