java - Sorting ArrayList using MyComparator Class -
i have hashmap copy arraylist , want sort entries in list according bandwidth of each entry, higher bandwidth first should come in list here tried
main class
arraylist<signal> messages = new arraylist<signal>(); messages.addall(map.values()); collections.sort(messages, new mycomparator());
my comparator class
public class mycomparator implements comparator<signal>{ @override public int compare(signal s1, signal s2) { if (s1.getbandwidth() > s2.getbandwidth() ) { return -1; } else if (s1.getbandwidth() < s2.getbandwidth()) { return 1; } return 0; } }
it not sort signal objects according bandwidth ?
what doing wrong
problem solved
actually not setting bandwidth before sorting it
here code hope 1
public void createsortedset(hashmap<string, signal> map, final bufferedwriter buffwriter, long totalsize) { try { messages.addall(map.values()); (signal signal : messages) { signal.setbandwidth((signal.getsize() / (float) totalsize) * 100); } collections.sort(messages, new mycomparator()); } catch (exception e) { e.printstacktrace(); } createcsv(buffwriter, messages); }
thanks
Comments
Post a Comment