Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import java.util.ArrayList;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Person firstPerson = new Person("Mariia", 19);
- Person secondPerson = new Person("Nikolas",24);
- Person thirdPerson = new Person("Elvira",20);
- Person fourthPerson = new Person("Olya",21);
- Person fifthPerson = new Person("Kate",22);
- Student firstStudent = new Student("Oleh", 20, 3, "standard");
- Student secondStudent = new Student("Olena", 18, 4, "increased");
- Student thirdStudent = new Student("Mikhail",22, 4, "increased");
- Student fourthStudent = new Student("Alex",21, 2, "standard");
- Student fifthStudent = new Student("Nikola",27, 2, "standard");
- ArrayList<Person> personArrayList = new ArrayList<Person>();
- personArrayList.add(firstPerson);
- personArrayList.add(secondPerson);
- personArrayList.add(thirdPerson);
- personArrayList.add(fourthPerson);
- personArrayList.add(fifthPerson);
- personArrayList.add(firstStudent);
- personArrayList.add(secondStudent);
- personArrayList.add(thirdStudent);
- personArrayList.add(fourthStudent);
- personArrayList.add(fifthStudent);
- for (int i = 0; i < personArrayList.size(); i++){
- System.out.println(personArrayList.get(i));
- }
- Comparator<Person> ageComp = new Comparator<Person>() {
- @Override
- public int compare(Person firstPerson, Person secondPerson) {
- Objects.requireNonNull(firstPerson);
- Objects.requireNonNull(secondPerson);
- if(firstPerson.getAge == secondPerson.getAge()) {
- return 0;
- } else if (firstPerson.getAge > secondPerson.getAge()){
- return -1;
- } else {
- return 1;
- }
- }
- };
- System.out.println("------------------------------------------------------");
- System.out.println("Age sorting:");
- personArrayList.sort(ageComp);
- for (int i = 0; i < personArrayList.size(); i++){
- System.out.println(personArrayList.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement