Custom sorting with multiple conditions using lambda Dry Run in PYTHON

Custom sorting with multiple conditions using lambda is an interactive PYTHON dry run visualizer from the Special programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Custom sorting with multiple conditions using lambda Program Code

employees = [
    ("Alice", "HR", 55000, 28),
    ("Bob", "IT", 75000, 35),
    ("Charlie", "IT", 68000, 29),
    ("Diana", "HR", 52000, 32),
    ("Eve", "Finance", 82000, 40),
    ("Frank", "IT", 72000, 31)
]

# Sort by department (ascending), then by salary (descending)
sorted_employees = sorted(employees, key=lambda x: (x[1], -x[2]))

# Sort by age ascending, then by name ascending
sorted_by_age = sorted(employees, key=lambda x: (x[3], x[0]))

print("Original order:")
for e in employees[:3]:
    print(f"  {e}")

print("\nSorted by Department (A-Z), then Salary (High-Low):")
for e in sorted_employees:
    print(f"  {e[1]}: {e[0]} - ${e[2]}")

print("\nSorted by Age (ascending), then Name:")
for e in sorted_by_age:
    print(f"  Age {e[3]}: {e[0]} ({e[1]})")

View the complete Special programs page.

Program Console Custom sorting with multiple conditions using lambda Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.