"how to sort the arraylist of objects in java" Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "how to sort the arraylist of objects in java" answered properly. Developers are finding an appropriate answer about how to sort the arraylist of objects in java related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like how to sort the arraylist of objects in java. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on how to sort the arraylist of objects in java.
how to sort the arraylist of objects in java
package beginnersbook.com;
import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<Student> arraylist = new ArrayList<Student>();
arraylist.add(new Student(101, "Zues", 26));
arraylist.add(new Student(505, "Abey", 24));
arraylist.add(new Student(809, "Vignesh", 32));
/*Sorting based on Student Name*/
System.out.println("Student Name Sorting:");
Collections.sort(arraylist, Student.StuNameComparator);
for(Student str: arraylist){
System.out.println(str);
}
/* Sorting on Rollno property*/
System.out.println("RollNum Sorting:");
Collections.sort(arraylist, Student.StuRollno);
for(Student str: arraylist){
System.out.println(str);
}
}
}
Source: beginnersbook.com
how to sort the arraylist of objects in java
package beginnersbook.com;
import java.util.Comparator;
public class Student {
private String studentname;
private int rollno;
private int studentage;
public Student(int rollno, String studentname, int studentage) {
this.rollno = rollno;
this.studentname = studentname;
this.studentage = studentage;
}
...
//Getter and setter methods same as the above examples
...
/*Comparator for sorting the list by Student Name*/
public static Comparator<Student> StuNameComparator = new Comparator<Student>() {
public int compare(Student s1, Student s2) {
String StudentName1 = s1.getStudentname().toUpperCase();
String StudentName2 = s2.getStudentname().toUpperCase();
//ascending order
return StudentName1.compareTo(StudentName2);
//descending order
//return StudentName2.compareTo(StudentName1);
}};
/*Comparator for sorting the list by roll no*/
public static Comparator<Student> StuRollno = new Comparator<Student>() {
public int compare(Student s1, Student s2) {
int rollno1 = s1.getRollno();
int rollno2 = s2.getRollno();
/*For ascending order*/
return rollno1-rollno2;
/*For descending order*/
//rollno2-rollno1;
}};
@Override
public String toString() {
return "[ rollno=" + rollno + ", name=" + studentname + ", age=" + studentage + "]";
}
}
Source: beginnersbook.com
All those coders who are working on the TypeScript based application and are stuck on how to sort the arraylist of objects in java can get a collection of related answers to their query. Programmers need to enter their query on how to sort the arraylist of objects in java related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to sort the arraylist of objects in java for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of how to sort the arraylist of objects in java while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "how to sort the arraylist of objects in java". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to sort the arraylist of objects in java resolved professionally and stay updated to the latest TypeScript updates.