Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Student {
- String name;
- int age, id;
- Student(String name, int age, int id) {
- this.name = name;
- this.age = age;
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public int getAge() {
- return this.age;
- }
- public int getId() {
- return this.id;
- }
- @Override
- public String toString() {
- return ("Name: " + this.getName() +
- ", Age: " + this.getAge() +
- ", Id: " + this.getId());
- }
- }
- public class Main {
- static void printList(ArrayList<Student> studentList) {
- for(Student s : studentList) {
- System.out.println(s);
- }
- }
- public static void main(String[] args) {
- ArrayList<Student> studentList = new ArrayList<Student>();
- studentList.add(new Student("Jon", 22, 1001));
- studentList.add(new Student("Steve", 19, 1003));
- studentList.add(new Student("Kevin", 23, 1005));
- studentList.add(new Student("Ron", 20, 1010));
- studentList.add(new Student("Lucy", 18, 1111));
- // original sequence
- System.out.println("Before sorting the student data:");
- printList(studentList);
- // sorting by age
- System.out.println("\nSorting by Age:");
- studentList.sort((Student s1, Student s2) -> s1.getAge() - s2.getAge());
- printList(studentList);
- // sorting by name
- System.out.println("\nSorting by name:");
- studentList.sort((Student s1, Student s2) -> s1.getName().compareTo(s2.getName()));
- printList(studentList);
- // sorting by id
- System.out.println("\nSorting by id:");
- studentList.sort((Student s1, Student s2) -> s1.getId() - s2.getId());
- printList(studentList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement