Hi, I ran into a problem while sorting an arrayList as shown below (and yes, I imported using):
Collections.sort(personer);
I have this list:
private List<Person> personer; public Register() { personer = new ArrayList<Person>(); }
But I got an error:
mittscript.java:45: cannot find symbol symbol: method sort (java.util.List) location: class java.util.Collections Collections.sort (personer);
I answered you in another java post - alphabetically (list)
I believe that if you do, fix your problems.
Collection<Person> listPeople = new ArrayList<Person>(); Person.java class will implement Comparablepublic class Person implements Comparable<Person>{ public int compareTo(Person person) { if(this.name != null && person.name != null){ return this.name.compareToIgnoreCase(person.name); } return 0; } } Once you have this, in the class you add people, when you are done adding, type:Collections.sort(listPeople);
Collection<Person> listPeople = new ArrayList<Person>();
Person.java class will implement Comparable
public class Person implements Comparable<Person>{ public int compareTo(Person person) { if(this.name != null && person.name != null){ return this.name.compareToIgnoreCase(person.name); } return 0; } }
Once you have this, in the class you add people, when you are done adding, type:
Collections.sort(listPeople);
Collections sort. Person Comparable, sort.JVM , Person "" "", .
Collections
sort
Person
Comparable
. .
, 1
class Person implements Comparable { ... } Collections.sort(list);
2
Collections.sort(list, myCustomComparator);
You have to implement Comparable < T > Interface
Comparable < T > Interface
Source: https://habr.com/ru/post/1781843/More articles:getting username set in UserData of form FormsAuthenticationTicket - c #Sorting ComputingMap? - javaSVN Plugin for Windows Explorer - svnPackaging problems revised - language-agnosticWhy does RubyMine 2.0.2 launch Webrick on different ports every time I launch / debug my application? - ruby-on-railsДинамическое определение jqGrid - jsonDoes the point of trying to catch finally block? - c #GDI + kills me with this "common mistake" - .netCharacter encodings in these files represent a different error in TFS - tfsProgramming a multi-file loopback device on Linux - linuxAll Articles