forked from starrohan999/Hacktoberfest-Accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate Frequency_Sort.java
37 lines (37 loc) · 1.17 KB
/
Create Frequency_Sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
This class implements sorting Strings based on their frequency.
The frequency is stored in the HashMap<String, ArrayList<Integer>>
Where the ArrayList<Integer> contains three items - Scores, Id, Correction
*/
import java.io.*;
import java.util.HashMap;
import java.util.Comparator;
import java.util.ArrayList;
public class PlaylistSort implements Comparator<String>{
int SCORES_INDEX = 0;
int TIE_INDEX = 2;
private HashMap<String, ArrayList<Integer>> frequency = new HashMap<String, ArrayList<Integer>>();
public PlaylistSort(HashMap<String, ArrayList<Integer>> arr) {
frequency = arr;
}
public int compare(String s1, String s2) {
if(frequency.get(s1).get(SCORES_INDEX) < frequency.get(s2).get(SCORES_INDEX)) {
return 1;
}
else if(frequency.get(s1).get(SCORES_INDEX) > frequency.get(s2).get(SCORES_INDEX)) {
return -1;
}
else {
//If two playlists have the same score, we need to break the tie..so we compare the tie scores..
if((frequency.get(s1).get(TIE_INDEX)) < (frequency.get(s2).get(TIE_INDEX))) {
return 1;
}
else if((frequency.get(s1).get(TIE_INDEX)) > (frequency.get(s2).get(TIE_INDEX))) {
return -1;
}
else {
return 0;
}
}
}
}