//////////// inner class /////////////////
class ValueComparator implements Comparator {
LinkedHashMap base;
public ValueComparator(LinkedHashMap
this.base = hMap;
}
public int compare(Object a, Object b) {
Myclass obj1 = (Myclass) base.get(a);
Myclass obj2 = (Myclass) base.get(b);
if( obj1.getRankDistance() > obj2.getRankDistance() ) {
return -1;
} else {
return 1;
}
}
}
class Myclass
{
double rankDistance;
DMFmotif motif;
public Myclass( double rankDistance, DMFmotif motif) {
super();
this.rankDistance = rankDistance;
this.motif = motif;
}
public double getRankDistance() {
return rankDistance;
}
public void setRankDistance(double rankDistance) {
this.rankDistance = rankDistance;
}
public DMFmotif getMotif() {
return motif;
}
public void setMotif(DMFmotif motif) {
this.motif = motif;
}
}
//////////////////////////////////////////////// HASH MAP /////////////
LinkedHashMap
ValueComparator vcomp = new ValueComparator(lhMap);
TreeMap
// 1. First insert valus into orig hashmap loop ....
for(int i=0 ; i< vecDMFmotif.size() ; i++) {
DMFmotif cur = vecDMFmotif.get(i);
lhMap.put( cur.getId() , new Myclass(cur.getRankValue() ,cur ) ) ;
}
// 2. // NOW SORTING IS DONE
for (String key:lhMap.keySet()) {
sorted_map.put(key, lhMap.get(key));
}
// 3. Write sorted values
Myclass inner;
Set set = sorted_map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
inner = (Myclass)me.getValue() ;
System.out.println( inner.getRankDistance() + "\t" + inner.getMotif().getId() );
}
Will it work with android
ReplyDelete