问题
So currently I'm working on a project to just help me practice java. I have a game that I followed a tutorial on and I've modified it a bit, now I'm trying to figure out a way to sort a text file with a String followed by an Integer. My previous attempts have all failed and I have yet to find an online resource that has actually worked. I've successfully sorted it alphabetically using the String but I want to sort it by the Integer value.
Text File example
abc 31
adc 100
ccd 211
ddc 99
The first column will hold the name and the second column is the score the player got in my game.
I am looking to have it sorted with the highest "score" or Integer value at the top, which will be later used to show a HighScore leaderboard.
Example Sort
ccd 211
adc 100
ddc 99
abc 31
I've attempted adding the information to an arrayList and using collection.sort(), also I tried a more in depth route to help me understand more how maps work by creating and adding each lines information into a map, creating a linked list with entrySet(), then using a collections.sort(list, new comparator()) this is where I believe my logic failed at some point or the fact I know vary little on maps.
Any help is really appreciated.
回答1:
As you said, you have successfully sorted it using String. So you know how to access the file and store it in Data Structures. So now lets concentrate on sorting based on Integer. Let us consider that String and Integer is stored in key_value HashMap.
HashMap<String, Integer> key_value = new HashMap<String, Integer>();
Now lets Sort it in Descending Order:
String display = "";
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(key_value.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
for (Map.Entry<String, Integer> temp : list) {
display += "\n " + temp.getKey() + " : " + temp.getValue();
}
For ascending order just change:
return (o2.getValue()).compareTo(o1.getValue());
to
return (o1.getValue()).compareTo(o2.getValue());
回答2:
It seems that you have already sorted your Strings from your explanation, so I will not do that:
String[] list = {"abc", "adc", "ccd", "ddc"};
Integer[] intList = {100, 211, 99, 31};
Arrays.sort(intList);
for (int i = 0; i < 4; i++) {
System.out.println(list[i] + ' ' + intList[i]);
}
We sort our Integer
array with util.Arrays
, and we iterate through our array and print.
In light of your edit, we can use a map by getting the keys and values from the map, sorting them, and then printing them, as follows:
Map<String, Integer> hm = new HashMap<String, Integer>();
hm.put("abc", 211);
hm.put("ddc", 100);
hm.put("adc", 31);
hm.put("ccd", 99);
List hmKeys = new ArrayList(hm.keySet());
Collections.sort(hmKeys);
List hmVals = new ArrayList(hm.values());
Collections.sort(hmVals);
来源:https://stackoverflow.com/questions/61744448/sort-a-text-file-with-a-string-and-integer