Top rank per group: Difference between revisions

m
(Updated to work with Nim 1.4: added missing parameter type. Renamed "top" as "printTop". Added a parameter "people" to the proc. Miscellaneous other changes.)
Line 952:
 
<lang dyalect>type Employee(name,id,salary,department)
 
func Employee.toString() {
"$\(this.::salary) (name: \(this.::name), id: \(this.::id), department: \(this.::department)"
}
 
constlet employees = [
Employee("Tyler Bennett","E10297",32000,"D101"),
Employee("John Rappl","E21437",47000,"D050"),
Line 972:
Employee("Timothy Grove","E16398",29900,"D190")
]
 
func topNSalaries(n) {
//We sort employees based on salary
employees.sort($1.by: (x,y) => x::salary - $0.y::salary)
let max =
const max = if n > employees.len() - 1 { employees.len() - 1 } else { n }
employees.len() - 1
} else {
n
}
for i in 0..max {
yield employees[i]
Line 982 ⟶ 987:
}
 
forvar eseq in= topNSalaries(5) {
for e in seq {
print(e)
}</lang>
Anonymous user