package ru.job4j.sort;

import java.util.*;

/**
 * Created on 18.07.17
 * Class for sorting user lists.
 * @author Wamdue
 * @version 1.0
 */
public class SortUser {
    /**
     * Sorting by age.
     * @param list - source list to convert in sorted set.
     * @return - sorted set.
     */
    public Set<User> sort (List<User> list) {
        return new TreeSet<>(list);
    }

    /**
     * Sorting by name length.
     * @param list - source list to sort.
     * @return sorted list by name length.
     */
    public List<User> sortNameLength (List<User> list) {
        ArrayList<User> arrayList = new ArrayList<>(list);
        arrayList.sort(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getName().length() > o2.getName().length() ? 1 :
                        o1.getName().length() < o2.getName().length() ? -1 : 0;
            }
        });
        return arrayList;
    }

    /**
     * Sorting by name length, then by age.
     * @param list - source list to compare.
     * @return sorted list.
     */
    public List<User> sortByAllFields (List<User> list) {
        ArrayList<User> arrayList = new ArrayList<>(list);
        Comparator<User> comparator = new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                int result = 0;

                if (o1.getName().length() > o2.getName().length()) {
                    result = 1;
                } else if (o1.getName().length() == o2.getName().length()) {
                    result = o1.getAge() > o2.getAge() ? 1 : o1.getAge() == o2.getAge() ? 0 : -1;
                } else {
                    result = -1;
                }
                return result;
            }
        };
        arrayList.sort(comparator);
        return arrayList;
    }
}
@@ -794,85 +794,10 @@
 h() 
-%3E o2.getName().length() ? 1 :%0A                        o1.getName().length() %3C
+- 
  o2.
@@ -818,17 +818,8 @@
 th()
- ? -1 : 0
 ;%0A  
@@ -1294,32 +1294,8 @@
 t = 
-0;%0A%0A                if (
 o1.g
@@ -1312,17 +1312,17 @@
 ength() 
-%3E
+-
  o2.getN
@@ -1327,37 +1327,34 @@
 tName().length()
-) %7B%0A 
+;%0A
                 
@@ -1345,35 +1345,32 @@
                 
-   
 result = 1;%0A    
@@ -1366,148 +1366,21 @@
 t = 
-1;%0A                %7D else if (o1.getName().length() == o2.getName().length()) %7B%0A                    result = o1.getAge() %3E o2.getAge() ? 1 :
+result == 0 ?
  o1.
@@ -1388,18 +1388,17 @@
 etAge() 
-==
+-
  o2.getA
@@ -1406,93 +1406,17 @@
 e() 
-? 0 : -1;%0A                %7D else %7B%0A                    result = -1;%0A                %7D
+: result;
 %0A   

refactor SortUser class, making sort code smaller, task #10036