Anonymous user
Top rank per group: Difference between revisions
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.
Alextretyak (talk | contribs) (Added 11l) |
(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 3,242:
<lang nim>import algorithm
type Record = tuple[name, id: string
var people
("
("
("
("
("
("
("
("
("
("
("
proc pcmp(a, b: Record): int =
result = cmp(a.department, b.department)
if result
result = cmp(b.salary, a.salary)
sort(people, pcmp)▼
proc printTop(people: openArray[Record]; n: Positive) =
var rank = 0
for i, p in people:
if i > 0 and p.department != people[i-1].department:
rank = 0
echo
if rank < n:
echo p.department, " ", p.salary, " ", p.name
inc rank▼
people.printTop(2)</lang>
▲ inc rank
{{out}}
<pre>D050 47000 John Rappl
D050 21900 Nathan Adams
|