0

I have an ArrayList with Student Objects (Name, Mark in Math, Mark in Physics). How can I sort the ArrayList and edit with first highest number/s, then second highest then third and so on. For example I have the 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 will look for the student with highest number, I got H. I edit the object like: "H", 97, 90 and save it back to that ArraList. Again I look for 2nd highest but here I have students "B" and "C", I can see they same number in math but in Physics C has highest number so I will edit "C", 96, 90 and save it back to the ArrayList and so on. Here which student already edited, I will not fetch them for edit and save thr and so on. IF someone please help me! Thanks in advance

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
logcat
  • 485
  • 12
  • 22
  • it sorted by bellow code, now if you please tell more about the next part written above? that is; which one will be edited, I will not get that one when search, since for some reason I will have to search the whole ArrayList again and again – logcat Jun 04 '14 at 00:35
  • if you please look here and suggestme http://stackoverflow.com/questions/24028238/edit-object-arraylist-in-java-again-and-again – logcat Jun 04 '14 at 02:58

2 Answers2

3

Implement a Comparable interface to your student object and specify how you want it to compare.

Here is a good example: http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

Marcus Gabilheri
  • 1,259
  • 1
  • 14
  • 26
2

You need to create a Comparator it can be anonymous or a class and in your Student do a get method for your mark so you can use it inside the comparator to compare the marks for 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();
            }
        });
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • You would need to add a secondary comparison: `int compare = s.getMathMark() - s2.getMathMark(); return compare != 0 ? compare : s.getPhysicsMark() - s2.getPhysicsMark();` – shmosel Jun 03 '14 at 23:16
  • cool ! it works on first block of code ! thanks a lot all of you:) – logcat Jun 03 '14 at 23:58
  • You could also just use `Collections.max()` instead of `Collections.sort()`. – Kevin Coppock Jun 04 '14 at 00:02
  • it sort, now if you please tell more about the next part written above? that is; which one will be edited, I will not get that one when search, since for some reason I will have to search the whole ArrayList again and again – logcat Jun 04 '14 at 00:14
  • if you please look here and suggestme http://stackoverflow.com/questions/24028238/edit-object-arraylist-in-java-again-and-again – logcat Jun 04 '14 at 02:58