Task

Find the top   N   salaries in each department,   where   N   is provided as a parameter.

Use this data as a formatted internal data structure (adapt it to your language-native idioms, rather than parse at runtime), or identify your external data source:

Employee Name,Employee ID,Salary,Department
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
George Woltman,E00127,53500,D101
Adam Smith,E63535,18000,D202
Claire Buckman,E39876,27800,D202
David McClellan,E04242,41500,D101
Rich Holcomb,E01234,49500,D202
Nathan Adams,E41298,21900,D050
Richard Potter,E43128,15900,D101
David Motsinger,E27002,19250,D202
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190



#!/usr/bin/env jsish
/* Top rank per group, in Jsish */
function top_rank(n) {
    var by_dept = group_by_dept(data);
    for (var dept in by_dept) {
        puts(dept);
        for (var i = 0; i < n && i < by_dept[dept].length; i++) {
            var emp = by_dept[dept][i];
            puts(emp.name + ", id=" + emp.id + ", salary=" + emp.salary);
        }
        puts("");
    }
}
 
// group by dept, and sort by salary
function group_by_dept(data) {
    var by_dept = {};
    for (var idx in data)  {
        var dept = data[idx].dept;
        if ( !by_dept.hasOwnProperty(dept)) {
            by_dept[dept] = new Array();
        }
        by_dept[dept].push(data[idx]);
    }
    for (var dept in by_dept) {
        by_dept[dept].sort(function (a,b) { return b.salary - a.salary; });
    }
    return by_dept;
}
 
if (Interp.conf('unitTest')) {
    var data = [
        {name: "Tyler Bennett",   id: "E10297", salary: 32000, dept: "D101"},
        {name: "John Rappl",      id: "E21437", salary: 47000, dept: "D050"},
        {name: "George Woltman",  id: "E00127", salary: 53500, dept: "D101"},
        {name: "Adam Smith",      id: "E63535", salary: 18000, dept: "D202"},
        {name: "Claire Buckman",  id: "E39876", salary: 27800, dept: "D202"},
        {name: "David McClellan", id: "E04242", salary: 41500, dept: "D101"},
        {name: "Rich Holcomb",    id: "E01234", salary: 49500, dept: "D202"},
        {name: "Nathan Adams",    id: "E41298", salary: 21900, dept: "D050"},
        {name: "Richard Potter",  id: "E43128", salary: 15900, dept: "D101"},
        {name: "David Motsinger", id: "E27002", salary: 19250, dept: "D202"},
        {name: "Tim Sampair",     id: "E03033", salary: 27000, dept: "D101"},
        {name: "Kim Arlich",      id: "E10001", salary: 57000, dept: "D190"},
        {name: "Timothy Grove",   id: "E16398", salary: 29900, dept: "D190"}
    ];
 
    top_rank(3);
}
 
/*
=!EXPECTSTART!=
D050
John Rappl, id=E21437, salary=47000
Nathan Adams, id=E41298, salary=21900
 
D101
George Woltman, id=E00127, salary=53500
David McClellan, id=E04242, salary=41500
Tyler Bennett, id=E10297, salary=32000
 
D190
Kim Arlich, id=E10001, salary=57000
Timothy Grove, id=E16398, salary=29900
 
D202
Rich Holcomb, id=E01234, salary=49500
Claire Buckman, id=E39876, salary=27800
David Motsinger, id=E27002, salary=19250
 
=!EXPECTEND!=
*/