Two-field Arraylist Sort (Java)

I have an archaist of records for the card game that I do. There must be a final limit on the number of points that this arraylist can store, which the user of the system can determine (say 10 at the moment). Fields in objects in arraylist are the player’s name (string), their score (int) and (almost) unique identifier for the player (Long, System.currentTimeMillis ()). The arrailist should be sorted by score, where the lowest result is the best. However, if all players in the arraist have the same score and a new player is added with this score, I would like players with the most recent results (those for which the highest IDs) to be saved before the older ones, so the old ones are discarded.

Essentially, I need a way to sort the ArrayList into two fields: first the grade, low to high, and then if the grade matches sorting by ID. Removing unnecessary elements, which I have already mainly considered, although if there is a way to integrate, I would be interested to hear it.

EDIT: I'm trying to sort Arraylist objects with these attributes, and not just one arraylist with them just throws. My bad.

+5
source share
3 answers

If you are using Java 8, there is a neat way to map attachments to method links:

List<Player> players = // ... players.sort(Comparator .comparing(Player::getScore) .thenComparing(Player::getId)); 

More information can be found in Comparator JavaDoc .

+6
source

Do not use ArrayList to store data.

Create a custom object with identifiers, names, ratings .... Then you create a custom Comparator ,

Here is an example of a simple user object and a custom Comparator. You must modify the Comparator to implement the logic you describe.

 /* ** Use the Collections API to sort a List for you. ** ** When your class has a "natural" sort order you can implement ** the Comparable interface. ** ** You can use an alternate sort order when you implement ** a Comparator for your class. */ import java.util.*; public class Person implements Comparable<Person> { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public String toString() { return name + " : " + age; } /* ** Implement the natural order for this class */ public int compareTo(Person p) { return getName().compareTo(p.getName()); } static class AgeComparator implements Comparator<Person> { public int compare(Person p1, Person p2) { return p1.getAge() - p2.getAge(); } } public static void main(String[] args) { List<Person> people = new ArrayList<Person>(); people.add( new Person("Homer", 38) ); people.add( new Person("Marge", 35) ); people.add( new Person("Bart", 15) ); people.add( new Person("Lisa", 13) ); // Sort by natural order Collections.sort(people); System.out.println("Sort by Natural order"); System.out.println("\t" + people); // Sort by reverse natural order Collections.sort(people, Collections.reverseOrder()); System.out.println("Sort by reverse natural order"); System.out.println("\t" + people); // Use a Comparator to sort by age Collections.sort(people, new Person.AgeComparator()); System.out.println("Sort using Age Comparator"); System.out.println("\t" + people); // Use a Comparator to sort by descending age Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator())); System.out.println("Sort using Reverse Age Comparator"); System.out.println("\t" + people); } } 
+3
source

You can use Collections.sort ()

  Class Student{ String fname=""; String lname=""; int age =0; int score=0; public Student(String fname,String lname,int age, int score) { this.fname=fname; this.lname=lname; this.age=age; this.score=score; } } 

Sort My List

  ArrayList<Student> list = new ArrayList<Student>(); /*add elements*/ Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student x, Student y) { if(x.score == y.score) { return (y.age-x.age); } else return (y.score-x.score); } }); 
+2
source

All Articles