Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- class Cat{
- int weight;
- int age;
- public Cat(int weight, int age) {
- this.weight = weight;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Cat{" +
- "weight=" + weight +
- ", age=" + age +
- '}';
- }
- }
- public class Main {
- public static void main(String[] args) {
- // long y []= new long[1L];
- int x [] = {3,2,1,4,5,6,7,82,4,5};
- Arrays.sort(x);
- System.out.println(Arrays.toString(x));
- ArrayList<Cat> cats = new ArrayList<>();
- cats.add(new Cat(2,5));
- cats.add(new Cat(22,5));
- cats.add(new Cat(20,5));
- cats.add(new Cat(3,3));
- cats.add(new Cat(1,2));
- cats.add(new Cat(23,2));
- //CATS -> {-1, 0, 1}
- // cats.sort((c1, c2) -> {
- // if(c1.age < c2.age){
- // return -1;
- // } else if(c1.age == c2.age){
- // return c1.weight - c2.weight;
- // } else {
- // return 1;
- // }
- // });
- Comparator<Cat> com = Comparator
- .comparingInt((Cat c) -> c.age)
- .thenComparing((Cat c) -> c.weight);
- // cats.sort(Comparator.comparingInt(c -> c.age).thenComparingInt(c -> c));
- cats.sort(com);
- System.out.println(cats);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement