java collection sort on the bases of another collection -


i have been asked question in interview:

class student{   int rollno;   string name;   list<mark> marks; }  class mark{   string subject;   int mark; } 

how student collection student has secured third highest marks?

since in collection can call collections.sort custom comparator so:

   collections.sort(studentlist, new comparator<student>() {        @override        public int compare(student a, student b) {            double mark_a = collections.max(a.marks);            double mark_b = collections.max(b.marks);            return double.compare(mark_a, mark_b);        }    }); 

then can return third student. (you may need change calculation method mark_a , mark_b depending on "highest marks" means... may want average)


Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -