I tried running:
public static final void main(final String[] args) {
final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y" }; // init
final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
return e2.getValue().compareTo(e1.getValue());
}
});
final SortedMap<String, Double> vMap = new TreeMap<String, Double>();
double curRank = 0;
for (final String vStr : vStrs) {
curRank = new Random().nextDouble() * 100.0; // replacing
// vertexRank.getVertexScore(vStr);
// for testing
vMap.put(vStr, curRank);
}
vertexCentralities.addAll(vMap.entrySet());
for (final Map.Entry<String, Double> entry : vertexCentralities) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
and the output was sorted by value:
A: 70.50008784770233
Z: 55.48252329485239
E: 37.31308600830347
Y: 32.534528844628255
T: 16.544965680467794
R: 12.258316023552872
Maybe your problem comes from somewhere else... like g.getVertices()
or vertexRank.getVertexScore(vStr)
EDIT:
I tried with duplicates values for the String
and for the double
:
final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" };
curRank = new Random().nextInt(3);
and it looks like no duplicates are allowed. Is this your problem?
EDIT:
Found a solution if you want to allow multiple entry with the same Double
:
Replace your SortedSet vertexCentralities
's comparator condition to:
final int bValue = e2.getValue().compareTo(e1.getValue());
return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());