Top rank per group: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(12 intermediate revisions by 9 users not shown)
Line 26:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V data = [(‘Tyler Bennett’, ‘E10297’, 32000, ‘D101’),
(‘John Rappl’, ‘E21437’, 47000, ‘D050’),
(‘George Woltman’, ‘E00127’, 53500, ‘D101’),
Line 50:
L(rec) sorted(recs, key' rec -> rec[2], reverse' 1B)[0 .< n]
print(‘ #<15 #<15 #<15 #<15 ’.format(rec[0], rec[1], rec[2], rec[3]))
print()</langsyntaxhighlight>
 
{{out}}
Line 78:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
DEFINE ENTRY_SIZE="8"
 
Line 182:
Sort()
TopRank(3)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Top_rank_per_group.png Screenshot from Atari 8-bit computer]
Line 207:
=={{header|Ada}}==
top.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Containers.Vectors;
with Ada.Text_IO;
 
Line 277:
end;
end loop;
end Top;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D050
John Rappl | E21437 | 47000
Line 294:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">Add_Employee(record employees, text name, id, integer salary, text department)
{
employees[name] = list(name, id, salary, department);
Line 347:
 
0;
}</langsyntaxhighlight>Run as:
<pre>aime rcs/top_rank_per_group c N 5</pre>{{out}}
<pre style="height:30ex;overflow:scroll">Department D050
Line 366:
David Motsinger | E27002 | 19250
Adam Smith | E63535 | 18000</pre>
 
=={{header|ALGOL 68}}==
Using code from the [[Sort using a custom comparator#ALGOL 68|ALGOL 68 sample in the Sort using a custom comparator task]].
<syntaxhighlight lang="algol68">
BEGIN # show the top rank salaries per department #
# NODE to hold the employee data - will be the MODE that is sorted #
MODE SITEM = STRUCT( STRING employee name
, STRING employee id
, INT salary
, STRING department
);
# ---- begin code from the quicksort using a custom comparator task ----- #
#--- Swap function ---#
PROC swap = (REF[]SITEM array, INT first, INT second) VOID:
(
SITEM temp := array[first];
array[first] := array[second];
array[second]:= temp
);
#--- Quick sort partition arg function with custom comparision function ---#
PROC quick = (REF[]SITEM array, INT first, INT last, PROC(SITEM,SITEM)INT compare) VOID:
(
INT smaller := first + 1,
larger := last;
SITEM pivot := array[first];
WHILE smaller <= larger DO
WHILE compare(array[smaller], pivot) < 0 AND smaller < last DO
smaller +:= 1
OD;
WHILE compare( array[larger], pivot) > 0 AND larger > first DO
larger -:= 1
OD;
IF smaller < larger THEN
swap(array, smaller, larger);
smaller +:= 1;
larger -:= 1
ELSE
smaller +:= 1
FI
OD;
swap(array, first, larger);
IF first < larger-1 THEN
quick(array, first, larger-1, compare)
FI;
IF last > larger +1 THEN
quick(array, larger+1, last, compare)
FI
);
#--- Quick sort array function with custom comparison function ---#
PROC quicksort = (REF[]SITEM array, PROC(SITEM,SITEM)INT compare) VOID:
(
IF UPB array > LWB array THEN
quick(array, LWB array, UPB array, compare)
FI
);
# ==== end code from the quicksort using a custom comparator task ===== #
 
# sort comparison routine - sort ascending department, descending salary #
PROC sort by department then salary = ( SITEM a, SITEM b )INT:
IF department OF a < department OF b THEN -1
ELIF department OF a > department OF b THEN 1
ELIF salary OF a > salary OF b THEN -1
ELIF salary OF a < salary OF b THEN 1
ELSE 0
FI # sort by department then salary # ;
 
# returns s blank padded on the right to at least w characters #
PROC pad right = ( STRING s, INT w )STRING:
IF INT len = ( UPB s - LWB s ) + 1; len >= w THEN s ELSE s + ( ( w - len ) * " " ) FI;
# shows the top n ranked salaries in each department #
PROC show top rank = ( []SITEM data, INT n )VOID:
BEGIN
# copy the data and sort it #
[ LWB data : UPB data ]SITEM sorted data := data;
quicksort( sorted data, sort by department then salary );
# show the top salaries per department #
INT d pos := LWB sorted data;
WHILE d pos <= UPB sorted data DO
STRING curr department := department OF sorted data[ d pos ];
print( ( "Department: ", curr department, newline ) );
INT e count := 0;
WHILE IF e count < n THEN
print( ( " "
, employee id OF sorted data[ d pos ]
, " "
, pad right( employee name
OF sorted data[ d pos ], 24 )
, " "
, whole( salary OF sorted data[ d pos ], -6 )
, newline
)
)
FI;
e count +:= 1;
d pos +:= 1;
IF d pos > UPB sorted data
THEN FALSE
ELSE curr department = department OF sorted data[ d pos ]
FI
DO SKIP OD;
print( ( newline ) )
OD
END # show top rank # ;
 
# employee data #
[]SITEM employees = ( ( "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" )
);
# show the top two salaries by department #
show top rank( employees, 2 )
END
</syntaxhighlight>
{{out}}
<pre>
Department: D050
E21437 John Rappl 47000
E41298 Nathan Adams 21900
 
Department: D101
E00127 George Woltman 53500
E04242 David McClellan 41500
 
Department: D190
E10001 Kim Arlich 57000
E16398 Timothy Grove 29900
 
Department: D202
E01234 Rich Holcomb 49500
E39876 Claire Buckman 27800
</pre>
 
=={{header|AppleScript}}==
It looks as if this task's requirements have changed — possibly more than once — since it was first set and named. As at the end of May 2021, it specifies the return of the top N ''salaries'' in each department and makes no mention at all of any of the other employee details. This makes it easier to decide how to handle the other thing it doesn't mention: what to return when two or more people in the same department are on the same pay. The interpretation here is "top N ''discrete'' salary values" (or of course fewer if a department doesn't have that many).
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later for these 'use' commands!.
use sorter : script ¬
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
on topNSalariesPerDepartment(employeeRecords, n)
Line 377 ⟶ 519:
set employeeCount to (count employeeRecords)
if ((employeeCount > 0) and (n > 0)) then
-- Sort a copy of the employee record list by department with descending subsorts on salary.
-- with descending subsorts on salary.
copy employeeRecords to employeeRecords
script comparer
on isGreater(a, b)
return ((a's department > b's department) or ((a's department = b's department) and (a's salary < b's salary)))¬
((a's department = b's department) and (a's salary < b's salary)))
end isGreater
end script
Line 388 ⟶ 532:
end considering
-- Initialise the output with data from the first record in the sorted list, then work through the rest of the list.
-- then work through the rest of the list.
set {department:previousDepartment, salary:previousSalary} to beginning of employeeRecords
set {padValuemv, topSalaries, counter} to {missing value"-", {previousSalary}, 1}
set end of output to {department:previousDepartment, salaries:topSalaries}
repeat with i from 2 to employeeCount
set {department:thisDepartment, salary:thisSalary} to item i of employeeRecords
if (thisDepartment is= previousDepartment) then
if ((thisSalary < previousSalary) and ((count topSalaries) < n)) then
-- Another record from the same department. Include its salary in the output if different from
-- the previous one's and if fewer than n salaries from the department have been included so far.
if ((thisSalary < previousSalary) and (counter < n)) then
set end of topSalaries to thisSalary
set counter to counter + 1
set previousSalary to thisSalary
end if
else
-- First record of the next department.
-- Pad out the previous department's salary list with missing values if it has fewer than n entries.
repeat (n - counter(count topSalaries)) times
set end of topSalaries to padValuemv
end repeat
-- Start a result record for the new department and add it to the output.
set topSalaries to {thisSalary}
set counter to 1
set end of output to {department:thisDepartment, salaries:topSalaries}
set previousDepartment to thisDepartment
Line 417 ⟶ 558:
end repeat
-- Pad the last department's salary list if necessary.
repeat (n - counter(count topSalaries)) times
set end of topSalaries to padValuemv
end repeat
end if
Line 425 ⟶ 566:
end topNSalariesPerDepartment
 
on demojoin(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set employeeRecords to {¬
{|name|:"Tyler Bennett", |ID|:"E10297", salary:32000, department:"D101"}, ¬
Line 444 ⟶ 593:
set n to 4
set topSalaryRecords to topNSalariesPerDepartment(employeeRecords, n)
--> eg. {{department:"D050", salaries:{47000, 21900, missing value, missing value}}, … }
-- Derive a text report from the result.
set report to {"Top " & n & " salaries per department:"}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
repeat with thisRecord in topSalaryRecords
set end of report to thisRecord's department & ": " & join(thisRecord's salaries, " ")
set i to n
repeat while (item i of thisRecord's salaries is missing value)
set item i of thisRecord's salaries to "-"
set i to i - 1
end repeat
set end of report to thisRecord's department & ": " & thisRecord's salaries
end repeat
setreturn AppleScript's text item delimiters tojoin(report, linefeed)
end task
set report to report as text
set AppleScript's text item delimiters to astid
return report
end demo
 
demotask()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Top 4 salaries per department:
D050: 47000 21900 - -
D101: 53500 41500 32000 27000
D190: 57000 29900 - -
D202: 49500 27800 19250 18000"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">printTopEmployees: function [count, entries][
data: read.csv.withHeaders entries
departments: sort unique map data 'row -> row\Department
Line 505 ⟶ 643:
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190
}</langsyntaxhighlight>
 
{{out}}
Line 534 ⟶ 672:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Departments = D050, D101, D190, D202
StringSplit, Department_, Departments, `,, %A_Space%
 
Line 589 ⟶ 727:
Employee_%m%_Salary := Salary
Employee_%m%_Dept := Department
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department: D050
---------------------------
Line 613 ⟶ 751:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TOP_RANK_PER_GROUP.AWK [n]
#
Line 659 ⟶ 797:
exit(0)
}
</syntaxhighlight>
</lang>
<p>Sample command and output:</p>
<pre>
Line 683 ⟶ 821:
=={{header|Bracmat}}==
Bracmat has no dedicated sorting functions. However, when evaluating algebraic expressions, Bracmat sorts factors in products and terms in sums. Moreover, Bracmat simplifies products by, amongst other transformations, collecting exponents of the same base into a single exponent, which is the sum of the collected exponents: <code>a^b*a^c</code> &rarr; <code>a^(b+c)</code>. This built-in behaviour is made use of in this solution.
<langsyntaxhighlight lang="bracmat"> (Tyler Bennett,E10297,32000,D101)
(John Rappl,E21437,47000,D050)
(George Woltman,E00127,53500,D101)
Line 722 ⟶ 860:
)
& toprank$(!employees.3)
& ;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Top 3 salaries per department.
Department D050:
Line 740 ⟶ 878:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 794 ⟶ 932:
top(2);
return 0;
}</langsyntaxhighlight>{{out}}
<pre>D050 47000: John Rappl
D050 21900: Nathan Adams
Line 809 ⟶ 947:
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 878 ⟶ 1,016:
}
}
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
Department: D101
Line 900 ⟶ 1,038:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
#include <set>
#include <list>
Line 1,013 ⟶ 1,151:
 
return 0;
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">In department D050
Name ID Salary Department
Line 1,037 ⟶ 1,175:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">class Employee(name, id, salary, dept) {
shared String name;
shared String id;
Line 1,078 ⟶ 1,216:
for (dept -> staff in employees.group(Employee.dept))
dept -> staff.sort(byDecreasing(Employee.salary)).take(n)
};</langsyntaxhighlight>
 
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="lisp">(use '[clojure.contrib.seq-utils :only (group-by)])
 
(defstruct employee :Name :ID :Salary :Department)
Line 1,107 ⟶ 1,245:
(doseq [e (take 3 (reverse (sort-by :Salary emps)))]
(println e)))
</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department: D050
{:Name John Rappl, :ID E21437, :Salary 47000, :Department D050}
Line 1,125 ⟶ 1,263:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun top-n-by-group (n data value-key group-key predicate &key (group-test 'eql))
(let ((not-pred (complement predicate))
(group-data (make-hash-table :test group-test)))
Line 1,158 ⟶ 1,296:
(second entry) (insert datum (second entry))))))
(dolist (datum data group-data)
(update-entry (entry (funcall group-key datum)) datum)))))</langsyntaxhighlight>
Example
<langsyntaxhighlight lang="lisp">> (defparameter *employee-data*
'(("Tyler Bennett" E10297 32000 D101)
("John Rappl" E21437 47000 D050)
Line 1,187 ⟶ 1,325:
D050 (2 (("Nathan Adams" E41298 21900 D050) ("John Rappl" E21437 47000 D050)))
D202 (3 (("David Motsinger" E27002 19250 D202) ("Claire Buckman" E39876 27800 D202) ("Rich Holcomb" E01234 49500 D202)))
D190 (2 (("Timothy Grove" E16398 29900 D190) ("Kim Arlich" E10001 57000 D190)))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.conv, std.range;
 
struct Employee {
Line 1,224 ⟶ 1,362:
writefln("Department %s\n %(%s\n %)\n", dep, recs.take(n));
}
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D202
Employee("Rich Holcomb", "E01234", 49500, "D202")
Line 1,242 ⟶ 1,380:
Employee("John Rappl", "E21437", 47000, "D050")
Employee("Nathan Adams", "E41298", 21900, "D050")</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
type TEmployeeInfo = record
Name,ID: string;
Salary: double;
Dept: string;
end;
 
var EmployeeInfo: array [0..12] of TEmployeeInfo = (
(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')
);
 
 
function SalarySort(Item1, Item2: Pointer): Integer;
var Info1,Info2: TEmployeeInfo;
begin
Info1:=TEmployeeInfo(Item1^);
Info2:=TEmployeeInfo(Item2^);
Result:=AnsiCompareStr(Info1.Dept,Info2.Dept);
If Result=0 then Result:=Trunc(Info1.Salary-Info2.Salary);
end;
 
procedure ShowTopSalaries(Memo: TMemo);
var List: TList;
var Info: TEmployeeInfo;
var I: integer;
var S,OldDept: string;
 
procedure NewDepartment(Name: string);
begin
Memo.Lines.Add('');
Memo.Lines.Add('Department: '+Name);
Memo.Lines.Add('Employee Name Employee ID Salary Department');
OldDept:=Name;
end;
 
 
begin
List:=TList.Create;
try
for I:=0 to High(EmployeeInfo) do
List.Add(@EmployeeInfo[I]);
List.Sort(SalarySort);
OldDept:='';
for I:=0 to List.Count-1 do
begin
Info:=TEmployeeInfo(List[I]^);
if OldDept<>Info.Dept then NewDepartment(Info.Dept);
S:=Format('%-18S %9S %11.0m %8S',[Info.Name,Info.ID,Info.Salary,Info.Dept]);
Memo.Lines.Add(S);
end;
 
finally List.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Department: D050
Employee Name Employee ID Salary Dept
Nathan Adams E41298 $21,900 D050
John Rappl E21437 $47,000 D050
 
Department: D101
Employee Name Employee ID Salary Dept
Richard Potter E43128 $15,900 D101
Tim Sampair E03033 $27,000 D101
Tyler Bennett E10297 $32,000 D101
David McClellan E04242 $41,500 D101
George Woltman E00127 $53,500 D101
 
Department: D190
Employee Name Employee ID Salary Dept
Timothy Grove E16398 $29,900 D190
Kim Arlich E10001 $57,000 D190
 
Department: D202
Employee Name Employee ID Salary Dept
Adam Smith E63535 $18,000 D202
David Motsinger E27002 $19,250 D202
Claire Buckman E39876 $27,800 D202
Rich Holcomb E01234 $49,500 D202
 
Elapsed Time: 35.284 ms.
</pre>
 
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">type Employee(name,id,salary,department) with Lookup
func Employee.ToString() {
Line 1,269 ⟶ 1,511:
func topNSalaries(n) {
//We sort employees based on salary
employees.Sort(by: (x,y) => y.salary - x.salary)
let max =
if n > employees.Length() - 1 {
Line 1,280 ⟶ 1,522:
}
}
 
var seq = topNSalaries(5)
for e in seq {
print(e)
}</langsyntaxhighlight>
 
{{out}}
Line 1,297 ⟶ 1,539:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">/** Turn a list of arrays into a list of maps with the given keys. */
def addKeys(keys, rows) {
def res := [].diverge()
Line 1,335 ⟶ 1,577:
out.println()
}
}</langsyntaxhighlight>
 
Note: This uses an append-and-then-sort to maintain the list of top N; a sorted insert or a proper [[wp: selection algorithm|selection algorithm]] would be more efficient. As long as <var>N</var> is small, this does not matter much; the algorithm is O(n) with respect to the data set.{{out}}
Line 1,363 ⟶ 1,605:
=={{header|EchoLisp}}==
We use the '''sql.lib''' to select, sort and group records from the table 'emps'. The output is appended in a new table, 'high'.
<langsyntaxhighlight lang="scheme">
(lib 'struct) ;; tables are based upon structures
(lib 'sql) ;; sql-select function
Line 1,378 ⟶ 1,620:
group-by emp.dept
order-by emp.salary desc limit N into high))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(define emps_file
'(("Tyler Bennett" E10297 32000 D101)
Line 1,420 ⟶ 1,662:
[3] D202 Rich Holcomb 49500
[4] D666 Simon Gallubert 42
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import system'collections;
import system'routines;
import extensions;
Line 1,432 ⟶ 1,674:
class Employee
{
prop string Name : prop;
prop string ID : prop;
prop int Salary : prop;
prop string Department : prop;
string toPrintable()
Line 1,442 ⟶ 1,684:
.writePaddingRight(ID, 12)
.writePaddingRight(Salary.toPrintable(), 12)
.write:(Department);
}
Line 1,448 ⟶ 1,690:
{
topNPerDepartment(n)
= self.groupBy::(x => x.Department ).selectBy::(x)
{
^ new {
Line 1,454 ⟶ 1,696:
Employees
= x.orderBy::(f,l => f.Salary > l.Salary ).top(n).summarize(new ArrayList());
}
};
Line 1,478 ⟶ 1,720:
};
employees.topNPerDepartment:(2).forEach::(info)
{
console.printLine("Department: ",info.Department);
info.Employees.forEach:(printingLn);
console.writeLine:("---------------------------------------------")
};
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,511 ⟶ 1,753:
=={{header|Elixir}}==
Quick implementation using piping and anonymous functions.
<langsyntaxhighlight Elixirlang="elixir">defmodule TopRank do
def per_groupe(data, n) do
String.split(data, ~r/(\n|\r\n|\r)/, trim: true)
Line 1,546 ⟶ 1,788:
Timothy Grove,E16398,29900,D190
"""
TopRank.per_groupe(data, 3)</langsyntaxhighlight>
 
{{out}}
Line 1,567 ⟶ 1,809:
 
=={{header|Erlang}}==
<langsyntaxhighlight Erlanglang="erlang"><-module( top_rank_per_group ).
 
-export( [employees/0, employees_in_department/2, highest_payed/2, task/1] ).
Line 1,606 ⟶ 1,848:
[io:fwrite( "~p ~p: ~p~n", [Department, Salery, Name]) || #employee{department=Department, salery=Salery, name=Name} <- Highest_payeds],
io:nl().
</langsyntaxhighlight>{{out}}
<pre><14> top_rank_per_group:task(2).
"D050" 53500: "George Woltman"
Line 1,621 ⟶ 1,863:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let data =
[
"Tyler Bennett", "E10297", 32000, "D101";
Line 1,640 ⟶ 1,882:
let topRank n =
Seq.groupBy (fun (_, _, _, d) -> d) data
|> Seq.map (snd >> Seq.sortBy (fun (_, _, s, _) -> -s) >> Seq.take n)</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors assocs fry io kernel math.parser sequences
sorting ;
IN: top-rank
Line 1,684 ⟶ 1,926:
] first-n-each
nl
] assoc-each ;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
Department D101:
Line 1,706 ⟶ 1,948:
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
<lang Forth>
\ Written in ANS-Forth; tested under VFX.
\ Requires the novice package: http://www.forth.org/novice.html
Line 1,836 ⟶ 2,078:
 
test-data kill-employee
</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 1,871 ⟶ 2,113:
 
=={{header|Fortran}}==
The example data can easily be declared via DATA statements, as with <langsyntaxhighlight Fortranlang="fortran"> DATA EMPLOYEE(1:3)/
1 GRIST("Tyler Bennett","E10297",32000,"D101"),
2 GRIST("John Rappl","E21437",47000,"D050"),
3 GRIST("George Woltman","E00127",53500,"D101")/
</syntaxhighlight>
</lang>
(just showing the first three), however this does not allow the preparation of the header line, because <code>GRIST("Employee Name","Employee ID","Salary","Department")</code> would be invalid since the "salary" entry is a floating-point number in the data aggregate, not text. The header line could be discarded, but this is in violation of the ideas of properly-described data files. So, the plan is to read the data from an input file containing the full example input, with its header line given special treatment. No attempt is made to detect or report improperly-formatted data. The READ statements could refer to an internally-defined block of text, but reading an actual disc file is more normal. Ad-hoc decisions are made for the size of the CHARACTER variables, that are "surely big enough" for the example data, likewise with the size of the array to hold all the records.
 
Line 1,886 ⟶ 2,128:
If the requirement had been to report the highest-ranked salary, the deed could have been done via the F90 function MAXLOC, which locates the array index of the (first-encountered?) maximum value of an array of values; further, it allows an additional MASK parameter which could be used to select only those entries having a particular "department" code. However, aside from the multiple scans that would be required for multiple departmental codes, there is the requirement for the top N salaries, and anyway, there would still be the need to identify the different departmental codes once only. Accordingly, the way ahead is the classic: sort the data and then work through the sorted sequence. Since standard Fortran provides no "sort" function in its standard libraries nor does it offer a pre-processor that might include a sort generator and the like to generate the specific code needed for a particular data aggregate and sort key specification, one must engage in some repetition. For this purpose, the "comb" sort is convenient with its code requiring just one comparison and one "swap" action, so in-line code is not troublesome. On the other hand, when dealing with compound sort keys, a three-way IF statement (or equivalent) is necessary whereby the first field of the sort key is compared with appropriate action for the < and > cases, but for the = case the comparison moves on to the second key. ''Three'' different consequences from the one comparison. Despite the deprecations of the modernisers this is still possible for numerical comparisons, but alas, character comparisons are not allowed in an arithmetic-IF statement - though one could struggle with ICHAR action. Thus, two comparisons are made where only one should suffice.
 
Finally, standard Fortran does not offer a means of receiving parameters as might be supplied when a code file is activated in a command-line environment. There may be special library routines with names such as GETARG, but they're not standard. So, a READ statement is employed. Or, one could rewrite this routine as a subroutine having one parameter so as to approximate the form of the specification more closely. Extra statements... <langsyntaxhighlight Fortranlang="fortran"> PROGRAM TOP RANK !Just do it.
CHARACTER*28 HEADING(4) !The first line is a header.
TYPE GRIST !But this is the stuff.
Line 1,954 ⟶ 2,196:
 
END !That was straightforward.
</syntaxhighlight>
</lang>
 
As ever, some tricky sizing of fields in the FORMAT statements, and a collection of ad-hoc constants. Cross-linking the integers via PARAMETER statements and the like would add a whole lot of blather, so if field sizes are changed, a fair amount of fiddling would follow. A more accomplished data processing routine would include this.
Line 1,982 ⟶ 2,224:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define N 3 'show the top three employees of each rank
 
'here is all the data to be read in
Line 2,038 ⟶ 2,280:
next x
print
next d</langsyntaxhighlight>
 
{{out}}<pre>
Line 2,061 ⟶ 2,303:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">data Employee( name, id, salary, dept )
 
employees = [
Line 2,087 ⟶ 2,329:
printf( " %-16s %6s %7d\n", e.name, e.id, e.salary )
println()</langsyntaxhighlight>
{{out}}
Line 2,110 ⟶ 2,352:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,230 ⟶ 2,472:
}
}
}</langsyntaxhighlight>
{{out|Output (with additional data demonstrating ties)}}
<pre style="height:30ex;overflow:scroll">
Line 2,253 ⟶ 2,495:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def displayRank(employees, number) {
employees.groupBy { it.Department }.sort().each { department, staff ->
println "Department $department"
Line 2,280 ⟶ 2,522:
[Name: 'Timothy Grove', ID: 'E16398', Salary: 29900, Department: 'D190']
]
displayRank(employees, 3)</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D050
Name ID Salary
Line 2,306 ⟶ 2,548:
====Data.List====
{{Works with|GHC|7.10.3}}
<langsyntaxhighlight lang="haskell">import Data.List (sortBy, groupBy)
 
import Text.Printf (printf)
Line 2,384 ⟶ 2,626:
putStrLn $ replicate 31 '='
mapM_ (traverse ((printf "%-16s %3s %10.2g\n" . name) <*> dep <*> sal)) $
firstN 3 dep sal employees</langsyntaxhighlight>
{{out}}
<pre>NAME DEP TIP
Line 2,406 ⟶ 2,648:
Alternatively, if we store the data in key-value maps, rather than in a cons list, we can use Map.lookup and Map.filter, Map.keys and Map.elems, to pull out the data and shape reports fairly flexibly.
 
<langsyntaxhighlight Haskelllang="haskell">import Data.List (intercalate, nub, sort, sortBy, transpose)
import qualified Data.Map as M
import Data.Maybe (fromJust)
Line 2,503 ⟶ 2,745:
)
<$> transpose rows
)</langsyntaxhighlight>
{{Out}}
<pre>de Bont C.A. AB 65000
Line 2,519 ⟶ 2,761:
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">CHARACTER source="Test.txt", outP='Top_rank.txt', fmt='A20,A8,i6,2x,A10'
CHARACTER name*20, employee_ID*10, department*10, temp*10
REAL :: idx(1), N_top_salaries=3
Line 2,548 ⟶ 2,790:
ENDDO
 
END</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
Line 2,566 ⟶ 2,808:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="icon">record Employee(name,id,salary,dept)
 
procedure getEmployees ()
Line 2,609 ⟶ 2,851:
every show_employee (!employeesInGroup[1:(1+min(N,*employeesInGroup))])
}
end</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
$ ./top-rank-by-group 2
Line 2,644 ⟶ 2,886:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Employee.bas"(N)
110 LET NR=100:LET X=0
120 STRING NAME$(1 TO NR)*20,ID$(1 TO NR)*6,DEPT$(1 TO NR)*4
Line 2,694 ⟶ 2,936:
580 IF J<=N THEN PRINT " ";NAME$(I);TAB(23);ID$(I),SALARY(I):LET J=J+1
590 NEXT
600 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
J has a rich set of primitive functions, which combine the power of an imperative language with the expressiveness of a declarative, SQL-like language:
 
<langsyntaxhighlight lang="j">NB. Dynamically generate convenience functions
('`',,;:^:_1: N=:{.Employees) =:, (_&{"1)`'' ([^:(_ -: ])L:0)"0 _~ i.# E =: {: Employees
NB. Show top six ranked employees in each dept
N , (<@:>"1@:|:@:((6 <. #) {. ] \: SALARY)/.~ DEPT) |: <"1&> E</langsyntaxhighlight>
<pre style="height:30ex;overflow:scroll">
+-----+-----+-----------------+------+
Line 2,763 ⟶ 3,005:
Named as a function where the (maximum) number of employees in each department is a parameter:
 
<langsyntaxhighlight lang="j">reportTopSalaries=: 3 :'N , (<@:>"1@:|:@:((y <. #) {. ] \: SALARY)/.~ DEPT) |: <"1&> E'</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
reportTopSalaries 2
Line 2,784 ⟶ 3,026:
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.io.File;
import java.util.*;
 
Line 2,832 ⟶ 3,074:
});
}
}</langsyntaxhighlight>
 
<pre style="height:30ex;overflow:scroll">Department D050
Line 2,855 ⟶ 3,097:
 
===Iterative Solution===
<langsyntaxhighlight lang="javascript">var data = [
{name: "Tyler Bennett", id: "E10297", salary: 32000, dept: "D101"},
{name: "John Rappl", id: "E21437", salary: 47000, dept: "D050"},
Line 2,913 ⟶ 3,155:
 
top_rank(3);
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex;overflow:scroll">D101
Line 2,937 ⟶ 3,179:
===Map and reduce===
====ES5====
<langsyntaxhighlight lang="javascript">var collectDept = function (arrOfObj) {
var collect = arrOfObj.reduce(function (rtnObj, obj) {
if (rtnObj[obj.dept] === undefined) {
Line 2,967 ⟶ 3,209:
return list.slice(0,n);
});
};</langsyntaxhighlight>
====ES6====
By composition of generic functions:
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 3,127 ⟶ 3,369:
topNSalariesPerDept(3, xs)
);
})();</langsyntaxhighlight>
{{Out}}
<pre>[
Line 3,144 ⟶ 3,386:
=={{header|jq}}==
The task description invites use of the "language-native" data structure, which for jq is JSON, and so the following assumes that the file data.json contains an array of objects, each having as keys the strings on the header line. Thus, the first object in the array looks like this:
<langsyntaxhighlight lang="json"> {
"Employee Name": "Tyler Bennett",
"Employee ID": "E10297",
"Salary": "32000",
"Department": "D101"
}</langsyntaxhighlight>
===Program===
<langsyntaxhighlight lang="jq">def top_rank_per_department(n):
group_by(.Department)
| reduce .[] as $dept
Line 3,161 ⟶ 3,403:
| ($dept[0] | .Department) as $dept
| . + [ { "Department": $dept, "top_salaries": $max } ] );
</syntaxhighlight>
</lang>
 
===Example===
With the above program, the top two salaries in each dapartment can be found as shown in the following transcript:<langsyntaxhighlight lang="json">
$ jq 'top_rank_per_department(2) data.json
[
Line 3,195 ⟶ 3,437:
]
}
]</langsyntaxhighlight>
 
=={{header|Jsish}}==
Based on Javascript, imperative solution
<langsyntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Top rank per group, in Jsish */
function top_rank(n) {
Line 3,270 ⟶ 3,512:
 
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 3,296 ⟶ 3,538:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6.0
 
using DataFrames
Line 3,354 ⟶ 3,596:
println("\n$cl:\n$data")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,394 ⟶ 3,636:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
data class Employee(val name: String, val id: String, val salary: Int, val dept: String)
Line 3,424 ⟶ 3,666:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,448 ⟶ 3,690:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
exec 2> /tmp/Top_rank_per_group.err
Line 3,523 ⟶ 3,765:
(( j++ ))
fi
done</langsyntaxhighlight>
{{out}}<pre>
D050 47000 E21437 John Rappl
Line 3,535 ⟶ 3,777:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">N = 2
 
lst = { { "Tyler Bennett","E10297",32000,"D101" },
Line 3,570 ⟶ 3,812:
end
print ""
end</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department: D050
John Rappl E21437 47000
Line 3,588 ⟶ 3,830:
 
=== Object-Oriented ===
<langsyntaxhighlight lang="lua">--Employee Class
local EmployeeMethods = {
Line 3,681 ⟶ 3,923:
print("\t"..tostring(Employee))
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,699 ⟶ 3,941:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
' erase stack of values, so we can add data
Line 3,772 ⟶ 4,014:
}
Checkit
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 3,797 ⟶ 4,039:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">InitialList ={{"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"},
Line 3,810 ⟶ 4,052:
 
Scan[((Print["Department ",#[[1,4]],"\n","Employee","\t","Id","\t","Salary"]&[#])&[#];
(Scan[Print[#[[1]],"\t",#[[2]],"\t",#[[3]]]&,#] )& [#])&,TrimmedList]</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll"><pre>Department D101
Line 3,833 ⟶ 4,075:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import algorithm
 
type Record = tuple[name, id: string; salary: int; department: string]
Line 3,867 ⟶ 4,109:
inc rank
 
people.printTop(2)</langsyntaxhighlight>
 
{{out}}
Line 3,883 ⟶ 4,125:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">open StdLabels
 
let to_string (name,_,s,_) = (Printf.sprintf "%s (%d)" name s)
Line 3,932 ⟶ 4,174:
let () =
toprank data 3;
;;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department: D190
Kim Arlich (57000), Timothy Grove (29900)
Line 3,953 ⟶ 4,195:
Then sort each department by salaries and keep only the first n elements.
 
<langsyntaxhighlight Oforthlang="oforth">Object Class new: Employee(name, id, salary, dep)
Employee method: initialize := dep := salary := id := name ;
Line 3,979 ⟶ 4,221:
#dep employees sortBy groupWith( #dep )
map(#[ sortBy(#[ salary neg ]) left(n) ]) apply(#println) ; </langsyntaxhighlight>
 
{{out}}
Line 3,991 ⟶ 4,233:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
%% Create a list of employee records.
Data = {Map
Line 4,036 ⟶ 4,278:
end
in
{Inspect {TopEarners Data 3}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
{{PARI/GP select}}
<langsyntaxhighlight lang="parigp">{V=[["Tyler Bennett","E10297",32000,"D101"],
["John Rappl","E21437",47000,"D050"],
["George Woltman","E00127",53500,"D101"],
Line 4,067 ⟶ 4,309:
};
 
top(2,V)</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free_Pascal}}
{{libheader|Classes}} {{libheader|Math}}
<langsyntaxhighlight lang="pascal">program TopRankPerGroup(output);
 
uses
Line 4,141 ⟶ 4,383:
end;
end.
</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">% ./TopRankPerGroup
Enter the number of ranks: 3
Line 4,164 ⟶ 4,406:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub zip {
my @a = @{shift()};
my @b = @{shift()};
Line 4,210 ⟶ 4,452:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>D050
Line 4,231 ⟶ 4,473:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant N=3
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span>
-- Employee Name,Employee ID,Salary,Department
enum /*NAME,*/ /*ID,*/ SAL=3, DEPT=4
<span style="color: #000080;font-style:italic;">-- Employee Name,Employee ID,Salary,Department</span>
constant employees = {{"Tyler Bennett", "E10297",32000,"D101"},
<span style="color: #008080;">enum</span> <span style="color: #000080;font-style:italic;">/*NAME,*/ /*ID,*/</span> <span style="color: #000000;">SAL</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DEPT</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span>
{"John Rappl", "E21437",47000,"D050"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">employees</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"Tyler Bennett"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E10297"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
{"George Woltman", "E00127",53500,"D101"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"John Rappl"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E21437"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">47000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D050"</span><span style="color: #0000FF;">},</span>
{"Adam Smith", "E63535",18000,"D202"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"George Woltman"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E00127"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">53500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
{"Claire Buckman", "E39876",27800,"D202"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Adam Smith"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E63535"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
{"David McClellan","E04242",41500,"D101"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Claire Buckman"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E39876"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">27800</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
{"Rich Holcomb", "E01234",49500,"D202"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"David McClellan"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"E04242"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">41500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
{"Nathan Adams", "E41298",21900,"D050"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Rich Holcomb"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E01234"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49500</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
{"Richard Potter", "E43128",15900,"D101"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Nathan Adams"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E41298"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D050"</span><span style="color: #0000FF;">},</span>
{"David Motsinger","E27002",19250,"D202"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Richard Potter"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E43128"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
{"Tim Sampair", "E03033",27000,"D101"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"David Motsinger"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"E27002"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19250</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D202"</span><span style="color: #0000FF;">},</span>
{"Kim Arlich", "E10001",57000,"D190"},
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Tim Sampair"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E03033"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">27000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D101"</span><span style="color: #0000FF;">},</span>
{"Timothy Grove", "E16398",29900,"D190"}}
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Kim Arlich"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E10001"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57000</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D190"</span><span style="color: #0000FF;">},</span>
 
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Timothy Grove"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E16398"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29900</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D190"</span><span style="color: #0000FF;">}}</span>
function by_dept_sal(integer i, integer j)
return compare(employees[i][DEPT]&-employees[i][SAL],
<span style="color: #008080;">function</span> <span style="color: #000000;">by_dept_sal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
employees[j][DEPT]&-employees[j][SAL])
<span style="color: #008080;">return</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]&-</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">SAL</span><span style="color: #0000FF;">],</span>
end function
<span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]&-</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">SAL</span><span style="color: #0000FF;">])</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence tags = custom_sort(routine_id("by_dept_sal"),tagset(length(employees)))
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">by_dept_sal</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">)))</span>
string lastdep = ""
integer dcount = 0
<span style="color: #004080;">string</span> <span style="color: #000000;">lastdep</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
printf(1,"Top %d salaries by department\n",{N})
<span style="color: #004080;">integer</span> <span style="color: #000000;">dcount</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for i=1 to length(employees) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Top %d salaries by department\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">N</span><span style="color: #0000FF;">})</span>
object emp = employees[tags[i]]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">employees</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if emp[DEPT]!=lastdep then
<span style="color: #004080;">object</span> <span style="color: #000000;">emp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">employees</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span>
lastdep = emp[DEPT]
<span style="color: #008080;">if</span> <span style="color: #000000;">emp</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">lastdep</span> <span style="color: #008080;">then</span>
dcount = 1
<span style="color: #000000;">lastdep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">emp</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DEPT</span><span style="color: #0000FF;">]</span>
printf(1,"\n")
<span style="color: #000000;">dcount</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
else
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
dcount += 1
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">dcount</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if dcount<=N then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?emp
<span style="color: #008080;">if</span> <span style="color: #000000;">dcount</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">N</span> <span style="color: #008080;">then</span>
end if
<span style="color: #0000FF;">?</span><span style="color: #000000;">emp</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 4,292 ⟶ 4,537:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$data = Array(
Array("Tyler Bennett","E10297",32000,"D101"),
Array("John Rappl","E21437",47000,"D050"),
Line 4,339 ⟶ 4,584:
echo '</pre>';
}
top_sal(3);</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">D050
Name ID Salary
Line 4,362 ⟶ 4,607:
David Motsinger E27002 19250
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
Emp = [
% 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"]
],
print_top_ranks(Emp,3),
nl.
 
print_top_ranks(Emp,N) =>
printf("Top %d ranks per department:\n", N),
foreach(Dept in [Dept : [_,_,_,Dept] in Emp].sort_remove_dups())
printf("Department %s\n", Dept),
Emp2 = sort_down([[Salary,Name] : [Name,_,Salary,D] in Emp, D = Dept]),
foreach({[Salary,Name],E} in zip(Emp2,1..Emp2.length), E <= N)
printf("%-20s %-10s\n", Name, Salary)
end,
nl
end.</syntaxhighlight>
 
{{out}}
<pre>Top 3 ranks per department:
Department D050
John Rappl 47000
Nathan Adams 21900
 
Department D101
George Woltman 53500
David McClellan 41500
Tyler Bennett 32000
 
Department D190
Kim Arlich 57000
Timothy Grove 29900
 
Department D202
Rich Holcomb 49500
Claire Buckman 27800
David Motsinger 19250</pre>
 
===Using data as facts===
<syntaxhighlight lang="picat">go2 =>
Emp = findall([Name,ID,Salary,Dept],emp(Name,ID,Salary,Dept)),
print_top_ranks(Emp,3),
nl.
 
emp("Tyler Bennett","E10297","32000","D101").
emp("John Rappl","E21437","47000","D050").
emp("George Woltman","E00127","53500","D101").
emp("Adam Smith","E63535","18000","D202").
emp("Claire Buckman","E39876","27800","D202").
emp("David McClellan","E04242","41500","D101").
emp("Rich Holcomb","E01234","49500","D202").
emp("Nathan Adams","E41298","21900","D050").
emp("Richard Potter","E43128","15900","D101").
emp("David Motsinger","E27002","19250","D202").
emp("Tim Sampair","E03033","27000","D101").
emp("Kim Arlich","E10001","57000","D190").
emp("Timothy Grove","E16398","29900","D190").</syntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp"># Employee Name, ID, Salary, Department
(de *Employees
("Tyler Bennett" E10297 32000 D101)
Line 4,390 ⟶ 4,707:
(prinl) ) ) )
 
(topEmployees 3)</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D101:
Name ID Salary
Line 4,414 ⟶ 4,731:
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">(subrg, stringrange, stringsize):
rank: procedure options (main); /* 10 November 2013 */
 
Line 4,496 ⟶ 4,813:
end;
put skip list ('FINISHED');
end rank;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">How many highest-paid employees do you want?
 
Line 4,532 ⟶ 4,849:
 
=={{header|PL/SQL}}==
<langsyntaxhighlight lang="plsql">create or replace procedure "Top rank per group"(TOP_N in pls_integer default 3) as
cursor CSR_EMP(TOP_N pls_integer) is
select case LINE
Line 4,639 ⟶ 4,956:
DBMS_OUTPUT.PUT_LINE(v_emp."Top rank per group");
end loop;
end;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">function New-Employee ($Name, $ID, $Salary, $Department) {
New-Object PSObject `
| Add-Member -PassThru NoteProperty EmployeeName $Name `
Line 4,673 ⟶ 4,990:
} `
| Format-Table -GroupBy Department
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">PS> Get-TopRank 2
 
Line 4,705 ⟶ 5,022:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">% emp(name,id,salary,dpt)
emp('Tyler Bennett','E10297',32000,'D101').
emp('John Rappl','E21437',47000,'D050').
Line 4,757 ⟶ 5,074:
writef(' ID: %w\t%w\tSalary: %w\n', [Id,Name,Sal]),
fail.
topDeps(_).</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">?- topDeps(2).
Department: D101
Line 4,775 ⟶ 5,092:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Structure Employees
Name$
ID$
Line 4,832 ⟶ 5,149:
NewList MyEmployees.Employees()
 
displayTopEarners(MyEmployees(), 3)</langsyntaxhighlight>
 
'''Save this as 'DataFile.txt and let the program open this file'''<pre>
Line 4,882 ⟶ 5,199:
=={{header|Python}}==
Python 2.7/3.x compatible.
<langsyntaxhighlight lang="python">from collections import defaultdict
from heapq import nlargest
Line 4,911 ⟶ 5,228:
for rec in nlargest(N, recs, key=lambda rec: rec[-2]):
print (format % rec)
print('')</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D050
Employee Name Employee ID Salary Department
Line 4,940 ⟶ 5,257:
Uses namedtuples for database records, and groupby builtin to group records by Department:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">from collections import namedtuple
from itertools import groupby
 
Line 4,973 ⟶ 5,290:
lambda record: record.Department
)
))</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">D050
DBRecord(Employee_Name='John Rappl', Employee_ID='E21437', Salary='47000', Department='D050')
Line 4,992 ⟶ 5,309:
=={{header|R}}==
First, read in the data.
<langsyntaxhighlight Rlang="r">dfr <- read.csv(tc <- textConnection(
"Employee Name,Employee ID,Salary,Department
Tyler Bennett,E10297,32000,D101
Line 5,006 ⟶ 5,323:
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190")); close(tc)</langsyntaxhighlight>
To just return the top salary, it's very simple using tapply.
<langsyntaxhighlight Rlang="r">with(dfr, tapply(Salary, Department, max))</langsyntaxhighlight>
To return N salaries, we replace max with our own function.
<langsyntaxhighlight Rlang="r">get.top.N.salaries <- function(N)
{
with(dfr, tapply(Salary, Department,
Line 5,022 ⟶ 5,339:
}
 
get.top.N.salaries(3)</langsyntaxhighlight>
$D050
[1] 47000 21900
Line 5,036 ⟶ 5,353:
 
To return the whole record for each of the top salaries, a different tack is required.
<langsyntaxhighlight Rlang="r">get.top.N.salaries2 <- function(N)
{
#Sort data frame by Department, then by Salary
Line 5,050 ⟶ 5,367:
})
}
get.top.N.salaries2(3)</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll"> $D050
Employee.Name Employee.ID Salary Department
Line 5,076 ⟶ 5,393:
With the dplyr package we can use group_by and top_n to select the top n records in each group.
 
<syntaxhighlight lang="r">
<lang R>
library(dplyr)
dfr %>%
group_by(Department) %>%
top_n(2, Salary)
</syntaxhighlight>
</lang>
Provides:
<pre style="height:30ex;overflow:scroll">
Line 5,097 ⟶ 5,414:
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(struct employee (name id salary dept))
Line 5,128 ⟶ 5,445:
(employee-name e)
(employee-id e))))
</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D101:
$53500: George Woltman (E00127)
Line 5,150 ⟶ 5,467:
This program also makes heavy use of method calls that start with dot. In Raku this means a method call on the current topic, <tt>$_</tt>, which is automatically set by any <tt>for</tt> or <tt>map</tt> construct that doesn't declare an explicit formal parameter on its closure.
 
<syntaxhighlight lang="raku" perl6line>my @data = do for q:to/---/.lines -> $line {
E10297 32000 D101 Tyler Bennett
E21437 47000 D050 John Rappl
Line 5,178 ⟶ 5,495:
say .< Dept Id Salary Name > for @es[^$N]:v;
}
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">
D050 E21437 47000 John Rappl
Line 5,196 ⟶ 5,513:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/*REXX program displays the top N salaries in each department (internal table). */
parse arg topN . /*get optional # for the top N salaries*/
if topN=='' | topN=="," then topN= 1 /*Not specified? Then use the default.*/
Line 5,232 ⟶ 5,549:
dept.h= /*make sure we see the employee again. */
end /*topN*/
end /*dep*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 2 </tt>}}
<pre>
Line 5,279 ⟶ 5,596:
 
===version 2===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 12.02.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 5,353 ⟶ 5,670:
exit: Say arg(1)
help: Say 'Syntax: rexx topsal [topn]'
Exit</langsyntaxhighlight>
'''output'''
<pre>13 employees, 4 departments: D101 D050 D202 D190
Line 5,372 ⟶ 5,689:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Top rank per group
 
Line 5,432 ⟶ 5,749:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 5,468 ⟶ 5,785:
Without much thought to report formatting:
{{works with|Ruby|2.2+}}
<langsyntaxhighlight lang="ruby">require "csv"
 
data = <<EOS
Line 5,500 ⟶ 5,817:
end
 
show_top_salaries_per_group(data, 3)</langsyntaxhighlight>
 
{{out}}
Line 5,524 ⟶ 5,841:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">perSal$ = "Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050;
George Woltman,E00127,53500,D101
Line 5,562 ⟶ 5,879:
end if
print word$(depSal$(i),1,",");chr$(9);word$(depSal$(i),2,",");chr$(9);word$(depSal$(i),3,",")
next i</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Employee Name ID Salary
 
Line 5,593 ⟶ 5,910:
The implementation counts with ties.
 
<langsyntaxhighlight Rustlang="rust">#[derive(Debug)]
struct Employee<S> {
// Allow S to be any suitable string representation
Line 5,685 ⟶ 6,002:
 
Ok(())
}</langsyntaxhighlight>
 
{{out}}
Line 5,711 ⟶ 6,028:
=={{header|Scala}}==
===Application idiomatic version===
<langsyntaxhighlight lang="scala">import scala.io.Source
import scala.language.implicitConversions
import scala.language.reflectiveCalls
Line 5,768 ⟶ 6,085:
.map(_.toString).mkString("\t", "\n\t", ""))
}
}</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Reporting top 3 salaries in each department.
 
Line 5,791 ⟶ 6,108:
===using SLICK ORM===
{{libheader|H2 Database Engine}} Version 1.3.1
<langsyntaxhighlight lang="scala">import scala.slick.driver.H2Driver.simple._
import scala.slick.lifted.ProvenShape
 
Line 5,942 ⟶ 6,259:
plainQuery.foreach(println(_))
} // session
} // TopNrankSLICK</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">Tot.15 Employees in 4 deps.Avg salary: 33336.67
Line 5,977 ⟶ 6,294:
=={{header|Scheme}}==
{{works with|Gauche Scheme}}
<langsyntaxhighlight Schemelang="scheme">(use gauche.record)
 
;; This will let us treat a list as though it is a structure (record).
Line 6,033 ⟶ 6,350:
(for-each
(pa$ print-record col-widths)
(take* matches n))))))</langsyntaxhighlight>
<b>Testing:</b>
<pre>
Line 6,078 ⟶ 6,395:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var data = <<'EOF'.lines.map{ <name id salary dept> ~Z .split(',') -> flat.to_h }
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
Line 6,104 ⟶ 6,421:
}
print "\n"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 6,130 ⟶ 6,447:
The following [[SMEQL]] example returns the top 6 earners in each department based on this table schema:
 
<langsyntaxhighlight lang="smeql">table: Employees
----------------
empID
dept
empName
salary</langsyntaxhighlight>
Source Code:
<langsyntaxhighlight lang="smeql">srt = orderBy(Employees, [dept, salary], order)
top = group(srt, [(dept) dept2, max(order) order])
join(srt, top, a.dept=b.dept2 and b.order - a.order < 6)</langsyntaxhighlight>
 
Note: Since SMEQL is a query language, it has no built-in I/O capability to prompt the user for the threshold number ("6" in this example). However, it would be possible to retrieve the threshold from another table, but would make the example a bit confusing. In practice, an application language would most likely pass the value to it as a parameter.
 
=={{header|SQL}}==
<langsyntaxhighlight lang="sql">create table EMP
(
EMP_ID varchar2(6 char),
Line 6,182 ⟶ 6,499:
insert into EMP (EMP_ID, EMP_NAME, DEPT_ID, SALARY)
values ('E16400','Timothy Grive','D190',29900);
COMMIT;</langsyntaxhighlight>
<langsyntaxhighlight lang="sql">select case LINE
when 10 then
'Tot.' || LPAD(POPULATION, 2) || ' Employees in ' || TIE_COUNT ||
Line 6,276 ⟶ 6,593:
where EMP4.DEPT_ID = EMP3.DEPT_ID
and EMP4.SALARY >= EMP3.SALARY))
order by DEPT_ID ,LINE ,SALARY desc, EMP_ID;</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Tot.15 employees in 4 deps.Avg salary: 33336.67
-
Line 6,309 ⟶ 6,626:
 
Since there is no requirement for the layout - using the data from the table above
<langsyntaxhighlight lang="sql">WITH ranked_emp AS (
SELECT emp_name
,dept_id
Line 6,322 ⟶ 6,639:
FROM ranked_emp
WHERE ranking <= 2
ORDER BY dept_id, ranking;</langsyntaxhighlight>
 
<pre style="height:30ex;overflow:scroll">
Line 6,340 ⟶ 6,657:
=={{header|Stata}}==
The following shows the top k salaries. If there are less than k employees in a department, this will print all salaries.
<langsyntaxhighlight lang="stata">import delimited employees.csv
local k 2
bysort department (salary): list salary if _N-_n<`k'</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight Swiftlang="swift">struct Employee {
var name: String
var id: String
Line 6,391 ⟶ 6,708:
 
print("\(dept)'s highest paid employees are: \n\t\(employeeString)")
}</langsyntaxhighlight>
 
{{out}}
Line 6,412 ⟶ 6,729:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
set text {Tyler Bennett,E10297,32000,D101
Line 6,445 ⟶ 6,762:
}
 
top_n_salaries 3 $data</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department D101
George Woltman E00127 53500
Line 6,469 ⟶ 6,786:
Version using built-in database-like functionality.
 
<langsyntaxhighlight lang="scheme">#lang transd
 
 
Line 6,506 ⟶ 6,823:
limit: N)
(for rec in recs do (textout rec "\n")))))))
}</langsyntaxhighlight>{{out}}
<pre>
[D050, E21437, John Rappl, 47000]
Line 6,519 ⟶ 6,836:
 
Version using untyped vectors.
<langsyntaxhighlight lang="scheme">
#lang transd
 
Line 6,563 ⟶ 6,880:
))
}
</langsyntaxhighlight>{{out}}
<pre>
[[John Rappl, E21437, 47000, D050]]
Line 6,576 ⟶ 6,893:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
MODE DATA
$$ SET dates=*
Line 6,622 ⟶ 6,939:
PRINT " "
ENDLOOP
ENDCOMPILE</langsyntaxhighlight>{{out}}
<pre style='height:30ex;overflow:scroll'>
Department D050
Line 6,662 ⟶ 6,979:
The data is in a file, exactly as given in the problem. Parameter N is accepted from command line.
 
<langsyntaxhighlight lang="txr">@(next :args)
@{n-param}
@(next "top-rank-per-group.dat")
Line 6,684 ⟶ 7,001:
@ (end)
@ (end)
@(end)</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">Department: D101
George Woltman (E00127) $ 53500
Line 6,704 ⟶ 7,021:
Descend into argument list:
 
<syntaxhighlight lang ="txr">@(next :args)</langsyntaxhighlight>
 
Collect first argument as <code>n-param</code> variable:
 
<syntaxhighlight lang ="txr">@{n-param}</langsyntaxhighlight>
 
Drill into data file:
 
<langsyntaxhighlight lang="txr">@(next "top-rank-per-group.dat")</langsyntaxhighlight>
 
Match header exactly:
 
<langsyntaxhighlight lang="txr">Employee Name,Employee ID,Salary,Department</langsyntaxhighlight>
 
Now iterate over the data, requiring a variable called <code>record</code> to be bound in each iteration, and suppress all other variables from emerging. In the body of the collect, bind four variables. Then use these four variables to create a four-element list which is bound to the variable <code>record</code>. The <code>int-str</code> function converts the textual variable <code>salary</code> to an integer:
 
<langsyntaxhighlight lang="txr">@(collect :vars (record))
@name,@id,@salary,@dept
@(bind record (@(int-str salary) dept name id))
@(end)
</syntaxhighlight>
</lang>
 
Next, we bind five variables to the output of some TXR Lisp code, which will return five lists:
 
<langsyntaxhighlight lang="txr">@(bind (dept salary dept2 name id)
@(let* ((n (int-str n-param))
(dept-hash [group-by second record :equal-based])
Line 6,734 ⟶ 7,051:
(ranked (collect-each ((rec (hash-values dept-hash)))
[apply mapcar list [[sort rec > first] 0..n]])))
(cons dept [apply mapcar list ranked])))</langsyntaxhighlight>
 
This code binds some successive variables. <code>n</code> is an integer conversion of the command line argument.
Line 6,745 ⟶ 7,062:
The reason for these transpositions is to convert the data into individual nested lists, once for each field. This is the format needed by the TXR <code>@(output)</code> clause:
 
<langsyntaxhighlight lang="txr">@(output)
@ (repeat)
Department: @dept
Line 6,752 ⟶ 7,069:
@ (end)
@ (end)
@(end)</langsyntaxhighlight>
 
Here, all these variables are individual lists. The <code>dept</code> variable is a flat list; one nesting of <code>@(repeat)</code> iterates over it. The other variables are nested lists; a nested repeat drills into these.
Line 6,762 ⟶ 7,079:
The output is identical to the previous version.
 
<langsyntaxhighlight lang="txr">@(next :args)
@{n-param}
@(next "top-rank-per-group.dat")
Line 6,779 ⟶ 7,096:
(put-line `Department: @d`)
(each ((r dept-recs))
(put-line ` @{r[2] 15} (@{r[3]}) $@{r[0] -6}`)))))</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 6,785 ⟶ 7,102:
sort each partition descending by the second to last, take the first
n strings in each partition, and display the list of them in a reasonably understandable form.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 6,810 ⟶ 7,127:
#show+
 
main = top3 data</langsyntaxhighlight>{{out}}
<pre style="height:30ex;overflow:scroll">D190
Kim Arlich,E10001,57000
Line 6,831 ⟶ 7,148:
=={{header|VBA}}==
Creating an Excel pivot table to display the top n salaried employees per department. Data is read from a file.
<langsyntaxhighlight lang="vb">Private Sub top_rank(filename As String, n As Integer)
Workbooks.OpenText filename:=filename, Comma:=True
Dim ws As Worksheet
Line 6,867 ⟶ 7,184:
Public Sub main()
top_rank filename:="D:\data.txt", n:=3
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 6,875 ⟶ 7,192:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Tuple
import "./sort" for Sort, Cmp
import "./seq" for Lst
import "./fmt" for Fmt
 
var Employee = Tuple.create("Employee", ["name", "id", "salary", "dept"])
Line 6,912 ⟶ 7,229:
topRanked.each { |e| Fmt.print("$-15s $s $d", e.name, e.id, e.salary) }
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 6,933 ⟶ 7,250:
Rich Holcomb E01234 49500
Claire Buckman E39876 27800
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Sort(Array, Field, Size); \Sort Array in descending order by Field
int Array, Field, Size, I, J, T;
[for J:= Size-1 downto 0 do
for I:= 0 to J-1 do
if Array(I,Field) < Array(I+1,Field) then
[T:= Array(I); Array(I):= Array(I+1); Array(I+1):= T];
];
 
int Data, I, I0, Dept, N, Cnt;
[Data:=[["Tyler Bennett", "E10297", 32000, 101],
["John Rappl", "E21437", 47000, 050],
["George Woltman", "E00127", 53500, 101],
["Adam Smith", "E63535", 18000, 202],
["Claire Buckman", "E39876", 27800, 202],
["David McClellan", "E04242", 41500, 101],
["Rich Holcomb", "E01234", 49500, 202],
["Nathan Adams", "E41298", 21900, 050],
["Richard Potter", "E43128", 15900, 101],
["David Motsinger", "E27002", 19250, 202],
["Tim Sampair", "E03033", 27000, 101],
["Kim Arlich", "E10001", 57000, 190],
["Timothy Grove", "E16398", 29900, 190],
[0, 0, 0, 000]]; \sentinel
I:= 0; \find number of employees = Data size
while Data(I,0) # 0 do I:= I+1;
Sort(Data, 3, I); \sort by department field (3)
I:= 0; \sort by salary within each department
while Data(I,0) do
[Dept:= Data(I,3);
I0:= I;
repeat I:= I+1 until Data(I,3) # Dept;
Sort(@Data(I0), 2, I-I0);
];
N:= IntIn(0); \get parameter
I:= 0;
loop [Dept:= Data(I,3); \for each department
Text(0, "Department D");
if Dept < 100 then ChOut(0, ^0); IntOut(0, Dept); Text(0, ":^m^j");
Cnt:= 0;
loop [if Cnt >= N then
while Data(I,3) = Dept do I:= I+1; \skip any remaining
if Data(I,3) # Dept then quit;
IntOut(0, Data(I,2)); Text(0, " "); \salary
Text(0, Data(I,1)); Text(0, " "); \ID
Text(0, Data(I,0)); CrLf(0); \name
Cnt:= Cnt+1;
I:= I+1;
];
if Data(I,0) = 0 then quit;
];
]</syntaxhighlight>
 
{{out}}
<pre>
3
Department D202:
49500 E01234 Rich Holcomb
27800 E39876 Claire Buckman
19250 E27002 David Motsinger
Department D190:
57000 E10001 Kim Arlich
29900 E16398 Timothy Grove
Department D101:
53500 E00127 George Woltman
41500 E04242 David McClellan
32000 E10297 Tyler Bennett
Department D050:
47000 E21437 John Rappl
21900 E41298 Nathan Adams
</pre>
 
=={{header|zkl}}==
This reads a data file, the contents of which are as given by the task description (minus the header). To protect privacy, identifying information has been stripped from the output (aka laziness enhanced coding).
<langsyntaxhighlight lang="zkl">fcn setAppend(d,[(key,data)]){ d[key]=d.find(key,T).append(data) } //-->(key,(data,data...))
 
fcn topNsalaries(n){
Line 6,950 ⟶ 7,339:
"%s: %s".fmt(d,ss.concat(",")).println();
}
}(3);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits