Advertisement
Marisichka

Main

Dec 16th, 2022
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package org.example;
  2. import java.util.ArrayList;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Person firstPerson = new Person("Mariia", 19);
  10.         Person secondPerson = new Person("Nikolas",24);
  11.         Person thirdPerson = new Person("Elvira",20);
  12.         Person fourthPerson = new Person("Olya",21);
  13.         Person fifthPerson = new Person("Kate",22);
  14.  
  15.         Student firstStudent = new Student("Oleh", 20, 3, "standard");
  16.         Student secondStudent = new Student("Olena", 18, 4, "increased");
  17.         Student thirdStudent = new Student("Mikhail",22, 4, "increased");
  18.         Student fourthStudent = new Student("Alex",21, 2, "standard");
  19.         Student fifthStudent = new Student("Nikola",27, 2, "standard");
  20.  
  21.         ArrayList<Person> personArrayList = new ArrayList<Person>();
  22.         personArrayList.add(firstPerson);
  23.         personArrayList.add(secondPerson);
  24.         personArrayList.add(thirdPerson);
  25.         personArrayList.add(fourthPerson);
  26.         personArrayList.add(fifthPerson);
  27.  
  28.         personArrayList.add(firstStudent);
  29.         personArrayList.add(secondStudent);
  30.         personArrayList.add(thirdStudent);
  31.         personArrayList.add(fourthStudent);
  32.         personArrayList.add(fifthStudent);
  33.  
  34.         for (int i = 0; i < personArrayList.size(); i++){
  35.             System.out.println(personArrayList.get(i));
  36.         }
  37.  
  38.         Comparator<Person> ageComp = new Comparator<Person>() {
  39.             @Override
  40.             public int compare(Person firstPerson, Person secondPerson) {
  41.                 Objects.requireNonNull(firstPerson);
  42.                 Objects.requireNonNull(secondPerson);
  43.  
  44.                 if(firstPerson.getAge == secondPerson.getAge()) {
  45.                     return 0;
  46.                 } else if (firstPerson.getAge > secondPerson.getAge()){
  47.                     return -1;
  48.                 } else {
  49.                     return 1;
  50.                 }
  51.             }
  52.         };
  53.  
  54.         System.out.println("------------------------------------------------------");
  55.         System.out.println("Age sorting:");
  56.  
  57.         personArrayList.sort(ageComp);
  58.         for (int i = 0; i < personArrayList.size(); i++){
  59.             System.out.println(personArrayList.get(i));
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement