Define an Employee record with name, hire date, and department, then sort employees by hire date.
← Classes and OOP · Ref: Q1204
Define a record with fields in the desired sort priority order. The 'default operator' generates <=> comparing fields in declaration order. Put hireDate first for chronological sorting.
FIELD ORDER MATTERS
Employee
hireDate as Date: Date() first field = primary sort key
employeeName as String: String() second field = secondary sort key
department as String: String() third field = tertiary sort key
SORT BY HIRE DATE
sorted <- cat employees | sort | collect as List of Employee
Since hireDate is declared first, the default <=> operator sorts chronologically. Stream pipelines use the type's <=> operator automatically.
See Q1199 for sorting by date+time. See Q1135 for record sort pipeline. See Q1195 for mixed-type record.
Example
defines module qa.classesandoop.employeehiredate defines record Employee hireDate as Date: Date() employeeName as String: String() department as String: String() Employee() as pure -> theHireDate as Date theName as String theDepartment as String this.hireDate :=: theHireDate this.employeeName :=: theName this.department :=: theDepartment default operator defines program EmployeeHireDateDemo() stdout <- Stdout() // === CREATE EMPLOYEES === employees <- List() of Employee employees += Employee(2024-06-15, "Alice", "Engineering") employees += Employee(2023-01-10, "Bob", "Marketing") employees += Employee(2024-01-05, "Carol", "Engineering") employees += Employee(2022-09-20, "Dave", "Operations") // === SORT BY HIRE DATE (field declaration order) === sorted <- cat employees | sort | collect as List of Employee stdout.println("Employees by seniority:") cat sorted > stdout // === MOST SENIOR (earliest hire date) === mostSenior <- cat employees | sort | head 1 | collect as List of Employee stdout.println("Most senior:") cat mostSenior > stdout // === NEWEST HIRES === newest <- cat employees | sort | tail 2 | collect as List of Employee stdout.println("Newest hires:") cat newest > stdout // === COMPARE TWO EMPLOYEES === alice <- Employee(2024-06-15, "Alice", "Engineering") bob <- Employee(2023-01-10, "Bob", "Marketing") if bob < alice stdout.println("Bob was hired before Alice")
Common mistakes
E06820 — The 'default operator' must be the LAST declaration in the record body. Placing fields or methods after it triggers E06820. See ek9 -h E06820 for details.
Incorrect:
default operator department as String: String() (fields after default operator)
Correct:
default operator defines program
Other ways to ask this
- Create an Employee record with a Date field and sort a list of employees chronologically.
- I need to define an employee type and order employees by when they were hired.
- In Java I'd use Comparator.comparing(Employee::getHireDate). How does EK9 sort by a date field?
- Build an Employee record with hire date and use a stream pipeline to sort by seniority.
Coming from another language?
Java: employees.stream().sorted(Comparator.comparing(Employee::getHireDate)). Python: sorted(employees, key=lambda e: e.hire_date). Rust: sort_by_key(|e| e.hire_date). Go: sort.Slice with custom less. EK9: declare hireDate first + default operator + cat | sort | collect.
Keywords: seniority, sort, default operator, hire date, chronological, stream, field order, employee, date, record