java - Multiple Field Collection Sort -
i have class arraylist contains value string word, string expandedword, double confidence, double support
i want sort arraylist based on confidence, , based on support.
i have succeed sort arraylist based on confidence, failed make new method sort arraylist based on support
this code sort based on confidence
public class expandedterm implements comparable<expandedterm> { string word; string expandedword; double support; double confidence; public expandedterm (string word,string expandedword, double confidence,double support){ this.word = word; this.expandedword = expandedword; this.support = support; this.confidence = confidence; } public string getword(){ return word; } public string expandedword(){ return expandedword; } public double getsupport(){ return support; } public double getconfidence(){ return confidence; } @override public int compareto(expandedterm conf) { return new double(this.confidence).compareto(new double(conf.confidence)); }
i failed make method compareto, sort based on support value. how sort first confidence, , make method sort support value?
user comparator this. compaarble provide functionality sort on single type basis. here link found when use comparable , comapartor
http://iandjava.blogspot.in/2012/10/comparable-and-comparator.html
use multiple comparartor
- one confidence
public class confidancecomparator implements comparator<expandedterm> { @override public int compare(final expandedterm o1, final expandedterm o2) { return new double(o1.confidence).compareto(new double(o2.confidence)); } }
- one support
public class supportcomparator implements comparator<expandedterm> { @override public int compare(final expandedterm o1, final expandedterm o2) { return new double(o1.support).compareto(new double(o2.support)); } }
and use collections.sort(<list>, <comparator>)
adn desired list.
this required when want sort either on confidance basis or support basis.
if required first sort on confidance basis , if confidance equal check support basis. comparable sufficient , is
public int compareto(expandedterm conf) { int compare = new double(this.confidence).compareto(new double(conf.confidence)); if (compare == 0) { compare = new double(this.support).compareto(new double(conf.support)); } return compare; }
Comments
Post a Comment