Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created March 7, 2011 02:12
Show Gist options
  • Save ts-3156/857971 to your computer and use it in GitHub Desktop.
Save ts-3156/857971 to your computer and use it in GitHub Desktop.
public static ArrayList<String> sortDescending(HashMap<String, Integer> map){
ArrayList<String> dataToReturn = new ArrayList<String>();
Set<String> keySet = map.keySet();
for(int i = 0; i < keySet.size(); i++){
int maxCount = -1;
String maxName = "";
for(String name: keySet){
boolean alreadyAdded = false;
for(String addedName: dataToReturn){
if(addedName.equals(name)){
alreadyAdded = true;
break;
}
}
if(alreadyAdded)
continue;
int curCount = map.get(name);
if(curCount > maxCount){
maxCount = curCount;
maxName = name;
}
}
if(maxCount != -1){
dataToReturn.add(maxName);
}
else
break;
}
return dataToReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment