How to sort and get highest value from Java Object/ArrayList -
this question has answer here:
- how compare objects multiple fields 18 answers
- java - getting max value arraylist of objects? 3 answers
i have arraylist student objects (name, mark in math, mark in physics). how can sort arraylist , edit first highest number/s, second highest third , on. example have object value below:
arraylist<> allstudents = new arraylist(); allstudents.add(new student("a", 80, 94) ); allstudents.add(new student("b", 98, 91) ); allstudents.add(new student("f", 70, 84) ); allstudents.add(new student("c", 98, 92) ); allstudents.add(new student("h", 99, 93) );
i student highest number, got h. edit object like: "h", 97, 90 , save arralist. again 2nd highest here have students "b" , "c", can see same number in math in physics c has highest number edit "c", 96, 90 , save arraylist , on. here student edited, not fetch them edit , save thr , on. if please me! in advance
you need create comparator can anonymous or class , in student
method mark can use inside comparator compare marks sorting
example:
arraylist<student> allstudents = new arraylist(); allstudents.add(new student("a", 80, 94)); allstudents.add(new student("b", 98, 91)); allstudents.add(new student("f", 70, 84)); allstudents.add(new student("c", 98, 92)); allstudents.add(new student("h", 99, 93)); collections.sort(allstudents, new comparator<student>() { public int compare(student s, student s2) { return s.getmark() - s2.getmark(); } });
Comments
Post a Comment