From 75f5785297345cab75e66eb8d249a77e04364319 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Tue, 15 Oct 2024 16:47:42 +0200 Subject: [PATCH] feat: sorts the map in descending order --- src/Main.java | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 4dd9fed..8d40cbf 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,4 @@ -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class Main { public static void main(String[] args) { @@ -9,8 +7,13 @@ public class Main { // Gets a map with the total occurrences for each domain Map domains = getDomainTotal(data); - + System.out.println(domains); + + // Order the map by descending value of the value + List> sortedDomains = sortDomainMapDesc(domains); + + System.out.println(sortedDomains); } /** @@ -44,4 +47,19 @@ public class Main { return domains; } + + /** + * Sorts the domains in descending order, from most occurrences to the least occurrences of domains. + * @param domains An unsorted map of domains with the amount that they occur + * @return Sorted list of Map Entries, in descending order of occurrences + */ + private static List> sortDomainMapDesc(Map domains) { + // Create a list from the map entries + List> sortedDomains = new ArrayList<>(domains.entrySet()); + + // Sort the list in descending order + sortedDomains.sort(Map.Entry.comparingByValue(Comparator.reverseOrder())); + + return sortedDomains; + } } \ No newline at end of file