---
title: "Write a python program to sort a list of objects based on a specific property."  
description: "Write a python program to sort a list of objects based on a specific property."  
author: "Steilla Mitchel"  
published: 2023-06-18  
updated: 2023-06-19  
canonical: https://www.mindstick.com/forum/158770/write-a-python-program-to-sort-a-list-of-objects-based-on-a-specific-property  
category: "python"  
tags: ["python", "programs"]  
reading_time: 2 minutes  

---

# Write a python program to sort a list of objects based on a specific property.

Write a [program to sort](https://www.mindstick.com/forum/157933/write-a-program-to-sort-an-array-of-integers-using-the-bubble-sort-algorithm) a list of [objects](https://www.mindstick.com/forum/145447/what-are-objects) based on a [specific property](https://www.mindstick.com/forum/158807/write-a-rust-program-to-sort-a-vector-of-objects-based-on-a-specific-property).

## Replies

### Reply by Aryan Kumar

```plaintext
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def __lt__(self, other):
    return self.age < other.age

def sort_by_age(people):
  """
  Sorts a list of people by age.

  Args:
    people: The list of people to sort.

  Returns:
    A sorted list of people.
  """

  sorted_people = sorted(people, key=lambda person: person.age)

  return sorted_people

if __name__ == "__main__":
  people = [
      Person("John Doe", 30),
      Person("Jane Doe", 25),
      Person("Peter Smith", 40),
      Person("Mary Smith", 35),
  ]

  sorted_people = sort_by_age(people)

  for person in sorted_people:
    print(person.name, person.age)
```

This [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) works by first defining a class called `Person`. This class represents a person with a name and an age. The class defines a `__lt__()` method that compares two people by their age. The method returns True if the first person is less than the second person, and False otherwise.

The `sort_by_age()` function takes a list of people as input and returns a sorted list of people. The function works by first using the `sorted()` function to sort the list of people. The `sorted()` function takes a key parameter that specifies a function to use for sorting the list. In this case, the key parameter is a lambda function that gets the age of a person. The `sorted()` function then returns a sorted list of people.

The main function of the program simply gets the list of people from the user and then calls the `sort_by_age()` function to sort the list of people. The main function then prints the sorted list of people to the console.

To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as `sort_by_age.py`, you can run it by typing the following command into the command line:

Code snippet

```plaintext
python sort_by_age.py
```

This will print the sorted list of people to the console.

Here is an example of the output of the program:

Code snippet

```plaintext
$ python sort_by_age.py
Jane Doe 25
John Doe 30
Mary Smith 35
Peter Smith 40
```

As you can see, the output of the program is a sorted list of people, with the people sorted by their age.


---

Original Source: https://www.mindstick.com/forum/158770/write-a-python-program-to-sort-a-list-of-objects-based-on-a-specific-property

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
