Last active
November 28, 2018 15:54
-
-
Save jjspace/9eec381a4b5a1e17222964add1a6e57b to your computer and use it in GitHub Desktop.
sorting initially by severity, then count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TODO: | |
* Implement updated sorting functionality. Sort by: | |
* - High risks (high to low) | |
* - No High Risks. Medium risks (high to low) | |
* - No High Risks. No Medium Risks. Low Risks (high to low) | |
**/ | |
// Expected sorting result: | |
// summaryBuckets.sort = [ | |
// summaryBuckets[1], | |
// summaryBuckets[0], | |
// summaryBuckets[4], | |
// summaryBuckets[3], | |
// summaryBuckets[2], | |
// summaryBuckets[5] | |
// ]; | |
const summaryBuckets = [ | |
{ | |
summaries: { | |
results: [ | |
{ count: 50, severity: "HIGH" }, | |
{ count: 1000, severity: "MEDIUM" }, | |
{ count: 100, severity: "LOW" } | |
] | |
} | |
}, | |
{ | |
summaries: { | |
results: [ | |
{ count: 100, severity: "HIGH" }, | |
{ count: 10, severity: "MEDIUM" }, | |
{ count: 10000, severity: "LOW" } | |
] | |
} | |
}, | |
{ | |
summaries: { | |
results: [ | |
{ count: 100, severity: "LOW" } | |
] | |
} | |
}, | |
{ | |
summaries: { | |
results: [ | |
{ count: 50, severity: "MEDIUM" }, | |
{ count: 45, severity: "LOW" } | |
] | |
} | |
}, | |
{ | |
summaries: { | |
results: [ | |
{ count: 100, severity: "MEDIUM" }, | |
{ count: 45, severity: "LOW" } | |
] | |
} | |
}, | |
{ | |
summaries: { | |
results: [ | |
{ count: 50, severity: "LOW" } | |
] | |
} | |
}, | |
]; | |
function filterForSeverity(results, severity) { | |
return results.filter(result => result.severity === severity); | |
} | |
function sortBySeverity(sumA, sumB) { | |
const { summaries: { results: resA }} = sumA; | |
const { summaries: { results: resB }} = sumB; | |
const highA = filterForSeverity(resA, 'HIGH'); | |
const medA = filterForSeverity(resA, 'MEDIUM'); | |
const lowA = filterForSeverity(resA, 'LOW'); | |
const hasHighA = highA.length > 0; | |
const hasMedA = medA.length > 0; | |
const hasLowA = lowA.length > 0; | |
const highB = filterForSeverity(resB, 'HIGH'); | |
const medB = filterForSeverity(resB, 'MEDIUM'); | |
const lowB = filterForSeverity(resB, 'LOW'); | |
const hasHighB = highB.length > 0; | |
const hasMedB = medB.length > 0; | |
const hasLowB = lowB.length > 0; | |
if (hasHighA && !hasHighB) { | |
// sort A lower if A is high but B is not | |
return -1; | |
} | |
else if (hasHighB && !hasHighA) { | |
// sort B lower if B is high but A is not | |
return 1; | |
} | |
else if (hasHighA && hasHighB) { | |
// both have high, sort by count descending | |
return highB[0].count - highA[0].count; | |
} | |
// by this point we know both dont have any high | |
else if (hasMedA && !hasMedB) { | |
// sort A lower if A is medium but B is not | |
return -1; | |
} | |
else if (hasMedB && !hasMedA) { | |
// sort B lower if B is medium but A is not | |
return 1; | |
} | |
else if (hasMedA && hasMedB) { | |
// both have med, sort by cound descending | |
return medB[0].count - medA[0].count; | |
} | |
// by this point we know both don't have any high or medium | |
else { | |
// sorty by the count | |
return lowB[0].count - lowA[0].count; | |
} | |
} | |
// duplicate because sort is in place | |
let sortedSummaryBuckets = [...summaryBuckets]; | |
sortedSummaryBuckets.sort(sortBySeverity); | |
let expectedSummaryBuckets = [ | |
summaryBuckets[1], | |
summaryBuckets[0], | |
summaryBuckets[4], | |
summaryBuckets[3], | |
summaryBuckets[2], | |
summaryBuckets[5] | |
]; | |
console.log('Original'); | |
console.table(summaryBuckets.map(sum => { | |
return sum.summaries.results.map(res => `${res.severity}(${res.count})`); | |
})); | |
console.log('Expected'); | |
console.table(expectedSummaryBuckets.map(sum => { | |
return sum.summaries.results.map(res => `${res.severity}(${res.count})`); | |
})); | |
console.log('Sorted'); | |
console.table(sortedSummaryBuckets.map(sum => { | |
return sum.summaries.results.map(res => `${res.severity}(${res.count})`); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment