符号表给出按键排序的结果,但我们如何按值对符号表进行排序。我使用Arrays.sort(st,st.get(key))
但给了我一个错误:
找不到符号:方法排序(ST,java.lang.Integer)
我的程序看起来像这样。仍然收到错误:
import java.util.Comparator;
import java.util.Arrays;
public class DictionaryCounter {
private final String key;
public DictionaryCounter (String key){
this.key = key;
}
public static class Frequency implements Comparator<DictionaryCounter>{
public int compare(DictionaryCounter x, DictionaryCounter y){
return x.get(key).compareTo(y.get(key));
}
}
public static void main(String[] args) {
ST<String, Integer> st = new ST<String, Integer>();
//String key;
while (!StdIn.isEmpty()) {
key = StdIn.readString();
if (!st.contains(key))
{ st.put(key, 1); }
else
{ st.put(key,st.get(key) + 1 ); }
}
Arrays.sort(st,new Frequency (key));
for (String s: st.keys()) {
System.out.println(s + " " + st.get(s));
}
}
}