Sort a risk register

A risk register lists every known risk. Read top to bottom, it should show what to fix first.

What to build

Read a list of risks and print them from the highest score to the lowest.

Requirements

  • Score = likelihood × impact.
  • Sort by score, highest first. When two scores are equal, sort by name in alphabetical order.
  • Print each risk as score level name, with the levels of the lesson: 15+ critical, 10+ high, 5+ medium, otherwise low.

Input

Line 1: the number of risks N. Then N lines, each name|likelihood|impact (the name may contain spaces).

Output

N lines, sorted.

Constraints

1 to 50 risks. Likelihood and impact are 1 to 5. Names are unique.

Example

Input:

3
Lab PC disk fails|3|2
Phishing|4|4
Server flood|2|5

Output:

16 critical Phishing
10 high Server flood
6 medium Lab PC disk fails

Why it works

Sorting by score puts the biggest risks first, and the name as a second key makes the order the same every time, even for ties.

Examples

Example 1 · Input
3
Lab PC disk fails|3|2
Phishing|4|4
Server flood|2|5
Expected output
16 critical Phishing
10 high Server flood
6 medium Lab PC disk fails
Example 2 · Input
2
b|2|3
a|3|2
Expected output
6 medium a
6 medium b

Hints

Hint 1

line.split("|") separates the three parts.

Hint 2

list.sort((a, b) => b.score - a.score || a.name.localeCompare(b.name)) sorts by score, then by name.

Hint 3

Compare names with < and > if you want to be sure of plain alphabetical order.

Solution

Show one correct solution (try it yourself first)
function level(score) {
  if (score >= 15) return "critical";
  if (score >= 10) return "high";
  if (score >= 5) return "medium";
  return "low";
}
const n = Number(prompt());
const risks = [];
for (let k = 0; k < n; k++) {
  const [name, l, i] = prompt().split("|");
  risks.push({ name: name.trim(), score: Number(l) * Number(i) });
}
risks.sort((a, b) => b.score - a.score || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
for (const r of risks) console.log(`${r.score} ${level(r.score)} ${r.name}`);

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Threats, vulnerabilities and risk →