sorting - Sort a list i java with two elements (type string and double) in Java -
i writing pension program , stuck.
the program looks this:
first read in file every line has name of person, age, , first deposit. use method called readfile that. inside method call upon class called class savingswhich in separate file calculate pension. have following problem: sort names according pensions (highest lowest) don't know how that.
here method in readfile class:
@suppresswarnings("resource") public void readfile(double rate) { while(scan1.hasnextline()) { string input = scan1.nextline(); scan2 = new scanner(input).usedelimiter("/"); string = scan2.next(); int b = scan2.nextint(); int c = scan2.nextint(); // calculate savings savings s = new savings(); s.totalsavings(a, b, c, rate); // add savings array } }
1st, create class person :
class person{ private string name; private int age; private bigdecimal firstdeposit; private bigdecimal pension; //setters , getters method } now create list hold information of every person :
list<person> personlist=new arraylist<person>(); now sort list based on pension :
collections.sort(personlist, new comparator<person>() { public int compare(person p1, person p2) { return p1.getpension().compareto(p2.getpension()); } }); given hint go problem, suggested other users, kindly go through basics of java.
Comments
Post a Comment