Siv Scripts

Solving Problems Using Code

Sat 25 August 2018

Sorting by Multiple Fields in Python

Posted by Aly Sivji in Quick Hits   

A couple of weeks ago, I stumbled across the Python Sorting HOW TO. Learned that Python can sort objects by multiple attributes using operator.attrgetter and operator.itemgetter.

In this Quick Hit, I will show the Pythonic way of sorting objects by multiple attributes.

Note: This example is leveraging Python 3.7 dataclasses for convenience. Objects can be of any type.

In [1]:
from dataclasses import dataclass
import operator
import random
import sys

from faker import Faker
fake = Faker()
In [2]:
sys.version_info
Out[2]:
sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
In [3]:
# Create employees to sort
@dataclass(order=True)
class Employee:
    name: str
    age: int
    title: str
    

employees = []
for idx in range(10):
    _name = fake.name()
    _title = fake.profile(fields="job")["job"]
    _age = random.randint(18, 55)
    employee = Employee(name=_name, title=_title, age=_age)
    employees.append(employee)
In [4]:
employees
Out[4]:
[Employee(name='Eduardo Thomas', age=23, title='Engineer, broadcasting (operations)'),
 Employee(name='Valerie Lopez', age=27, title='Multimedia programmer'),
 Employee(name='Mrs. Stephanie Taylor', age=20, title='Engineer, building services'),
 Employee(name='Sandra Saunders', age=30, title='Fast food restaurant manager'),
 Employee(name='Victor Flores', age=30, title='Records manager'),
 Employee(name='Maria Richardson', age=47, title='Tour manager'),
 Employee(name='Jordan Romero', age=23, title='Production manager'),
 Employee(name='Jordan Carter', age=30, title='Tax adviser'),
 Employee(name='Katie Black', age=24, title='IT sales professional'),
 Employee(name='Maria Henderson', age=18, title='Contracting civil engineer')]
In [5]:
sorted(employees)
Out[5]:
[Employee(name='Eduardo Thomas', age=23, title='Engineer, broadcasting (operations)'),
 Employee(name='Jordan Carter', age=30, title='Tax adviser'),
 Employee(name='Jordan Romero', age=23, title='Production manager'),
 Employee(name='Katie Black', age=24, title='IT sales professional'),
 Employee(name='Maria Henderson', age=18, title='Contracting civil engineer'),
 Employee(name='Maria Richardson', age=47, title='Tour manager'),
 Employee(name='Mrs. Stephanie Taylor', age=20, title='Engineer, building services'),
 Employee(name='Sandra Saunders', age=30, title='Fast food restaurant manager'),
 Employee(name='Valerie Lopez', age=27, title='Multimedia programmer'),
 Employee(name='Victor Flores', age=30, title='Records manager')]
In [6]:
sorted(employees, key=operator.attrgetter("age"))
Out[6]:
[Employee(name='Maria Henderson', age=18, title='Contracting civil engineer'),
 Employee(name='Mrs. Stephanie Taylor', age=20, title='Engineer, building services'),
 Employee(name='Eduardo Thomas', age=23, title='Engineer, broadcasting (operations)'),
 Employee(name='Jordan Romero', age=23, title='Production manager'),
 Employee(name='Katie Black', age=24, title='IT sales professional'),
 Employee(name='Valerie Lopez', age=27, title='Multimedia programmer'),
 Employee(name='Sandra Saunders', age=30, title='Fast food restaurant manager'),
 Employee(name='Victor Flores', age=30, title='Records manager'),
 Employee(name='Jordan Carter', age=30, title='Tax adviser'),
 Employee(name='Maria Richardson', age=47, title='Tour manager')]
In [7]:
sorted(employees, key=operator.attrgetter("age", "name"))
Out[7]:
[Employee(name='Maria Henderson', age=18, title='Contracting civil engineer'),
 Employee(name='Mrs. Stephanie Taylor', age=20, title='Engineer, building services'),
 Employee(name='Eduardo Thomas', age=23, title='Engineer, broadcasting (operations)'),
 Employee(name='Jordan Romero', age=23, title='Production manager'),
 Employee(name='Katie Black', age=24, title='IT sales professional'),
 Employee(name='Valerie Lopez', age=27, title='Multimedia programmer'),
 Employee(name='Jordan Carter', age=30, title='Tax adviser'),
 Employee(name='Sandra Saunders', age=30, title='Fast food restaurant manager'),
 Employee(name='Victor Flores', age=30, title='Records manager'),
 Employee(name='Maria Richardson', age=47, title='Tour manager')]

Notice the employees are sorted by age and by name.


 
    
 
 

Comments