How to sort and get highest value from Java Object/ArrayList [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/369512/how-to-compare-objects-by-multiple-fields" dir="ltr" rel="nofollow">How to compare objects by multiple fields</a> <span class="question-originals-answer-count"> 21 answers </span> </li> <li> <a href="/questions/19338686/java-getting-max-value-from-an-arraylist-of-objects" dir="ltr" rel="nofollow">Java - getting max value from an arraylist of objects?</a> <span class="question-originals-answer-count"> 4 answers </span> </li> </ul>

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


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


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(); } });


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

Here is a good example: <a href="http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/" rel="nofollow">http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/</a>


  • raycast wont hit collider after using NGUI?
  • Learning about game development, any books recommended? [closed]
  • Why do DirectX fullscreen applications give black screenshots?
  • using flyway to patch multiple identical schemas
  • BeautifulSoup find and find_all not working as expect
  • Working with EditorTemplates and radio buttons
  • Mysql join four tables and show NULL value
  • else if statement in javascript not able to display validation message
  • How to sort results based on join table in Rails?
  • SQL: Find top-rated article in each category
  • How to find the largest array from a multi dimensional array [duplicate]
  • C++/VS2005: Defining the same class name in two different .cpp files
  • Linked tables and Slicer in excel
  • How to filter entities that are deleted using linq to entities
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Creating a Multidimensional, Associative Array in VBScript
  • C#: Import/Export Settings into/from a File
  • Can't remove headers after they are sent
  • How can I set a binding to a Combox in a UserControl?
  • How can I sort a a table with VBA with given text condition?
  • AJAX Html Editor Extender upload image appearing blank
  • Is it possible to access block's scope in method?
  • Is there a javascript serializer for JSON.Net?
  • Fetching methods from BroadcastReceiver to update UI
  • Where to put my custom functions in Wordpress?
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Symfony2: How to get request parameter
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • Buffer size for converting unsigned long to string
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Does armcc optimizes non-volatile variables with -O0?
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal