Skip to content
Advertisement

Python custom function for sorting a list of objects

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

JavaScript

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:

JavaScript

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

JavaScript

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

An example of desired output:

JavaScript

What function to use to satisfy the stated condition?

Advertisement

Answer

Try this.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement