Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created July 16, 2011 06:36
Show Gist options
  • Save iporsut/1086077 to your computer and use it in GitHub Desktop.
Save iporsut/1086077 to your computer and use it in GitHub Desktop.
Word Sorting
package main;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class WordSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
Map<Integer,List<String>> map = new HashMap<Integer, List<String>>();
while(scan.hasNext()) {
String s = scan.next();
if( s.equals("0")) break;
int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum += s.charAt(i) - 'a' + 1;
}
if(!map.containsKey(sum)) {
map.put(sum, new LinkedList<String>());
}
map.get(sum).add(s);
}
List <Integer>sortedKey = new LinkedList<Integer>(map.keySet());
Collections.sort(sortedKey);
for (Integer k : sortedKey) {
List <String>l = map.get(k);
for (String s : l) {
System.out.println(s);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment