package ru.job4j.fin;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Wamdue
 * @version 1.0
 * @since 24.07.2017
 */
public class ArraySort {
    /**
     * Constant to ascending order.
     */
    public static final int ASC = 0;
    /**
     * Constant to descending order.
     */
    public static final int DESC = 1;

    /**
     * Sort array in two ways.
     * @param array - source array.
     * @param method - sort method.
     * @return sorted list.
     */
    public ArrayList<String> sortedArray(String[] array, int method) {
        ArrayList<String> list = new ArrayList<>(arrayToSet(array));
        if (method != ArraySort.ASC) {
            list.sort((o1, o2) -> {
                String s1 = o1.replaceAll("[^0-9]", "");
                String s2 = o2.replaceAll("[^0-9]", "");
                s1 = s1.length() == 1 ? s1 + "99" : s1.length() == 2 ? s1 + "9" : s1;
                s2 = s2.length() == 1 ? s2 + "99" : s2.length() == 2 ? s2 + "9" : s2;
                return Integer.valueOf(s2) - Integer.valueOf(s1);
            });
        }
        return list;
    }

    /**
     * Method to add absent items in set, and ascending sort.
     * @param array - source array.
     * @return - sorted set.
     */
    private Set<String> arrayToSet(String[] array) {
        Set<String> set =  new TreeSet<>();

        for (String string : array) {
            String[] parts = string.split("\\\\");
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < parts.length; j++) {
                if (j > 0) {
                    sb.append("\\");
                }
                sb.append(parts[j]);
                set.add(sb.toString());
            }
        }
        return set;
    }
}
@@ -44,16 +44,45 @@
 ayList;%0A
+import java.util.Comparator;%0A
 import j
@@ -627,22 +627,19 @@
 rayList%3C
-String
+Dep
 %3E list =
@@ -740,76 +740,71 @@
 ort(
-(o1, o2) -%3E %7B%0A                String s1 = o1.replaceAll(%22%5B%5E0-9%5D%22, %22%22
+this.asc());%0A        %7D else %7B%0A            list.sort(this.desc()
 );%0A 
@@ -806,32 +806,35 @@
 ));%0A        
+%7D%0A%0A
         
 String s2 = 
@@ -825,57 +825,63 @@
     
-String s2 = o2.replaceAll(%22%5B%5E0-9%5D%22, %22%22
+ArrayList%3CString%3E result = new ArrayList%3C%3E(
 );%0A
+%0A
         
     
@@ -880,84 +880,59 @@
     
-        s1 = s1.length() == 1 ? s1 + %2299%22 : s1.length() == 2 ? s1 + %229%22 : s1
+for (Dep d : list) %7B%0A            result.add(d.name)
 ;%0A  
@@ -937,24 +937,27 @@
         
+%7D%0A%0A
         
 s2 = s2.
@@ -952,189 +952,208 @@
     
-s2 = s2.length() == 1 ? s2 + %2299%22 : s2.length() == 2 ? s2 + %229%22 : s2;%0A                return Integer.valueOf(s2) - Integer.valueOf(s1);%0A        
+return result;%0A    %7D%0A%0A    private Comparator%3CDep%3E asc() %7B%0A        return (o1, o2) -%3E o2.number - o1.number;%0A
     %7D
-);
+%0A
 %0A    
-    %7D%0A        return list
+private Comparator%3CDep%3E desc() %7B%0A        return Comparator.comparing(o -%3E o.name)
 ;%0A  
@@ -1153,24 +1153,25 @@
 me);%0A    %7D%0A%0A
+%0A
     /**%0A    
@@ -1317,22 +1317,19 @@
 ate Set%3C
-String
+Dep
 %3E arrayT
@@ -1363,22 +1363,19 @@
     Set%3C
-String
+Dep
 %3E set = 
@@ -1728,75 +1728,1072 @@
     
-set.add(sb.toString());%0A            %7D%0A        %7D%0A        return set;
+String val = sb.toString().replaceAll(%22%5B%5E0-9%5D%22, %22%22);%0A                val = val.length() == 1 ? val + %2299%22 : val.length() == 2 ? val + %229%22 : val;%0A                set.add(new Dep(sb.toString(), Integer.valueOf(val)));%0A            %7D%0A        %7D%0A        return set;%0A    %7D%0A%0A    private class Dep implements Comparable%3CDep%3E %7B%0A        String name;%0A        int number;%0A%0A        Dep(String name, int number) %7B%0A            this.name = name;%0A            this.number = number;%0A        %7D%0A%0A        @Override%0A        public boolean equals(Object o) %7B%0A            if (this == o) return true;%0A            if (o == null %7C%7C getClass() != o.getClass()) return false;%0A%0A            Dep dep = (Dep) o;%0A%0A            if (number != dep.number) return false;%0A            return name.equals(dep.name);%0A        %7D%0A%0A        @Override%0A        public int hashCode() %7B%0A            int result = name.hashCode();%0A            result = 31 * result + number;%0A            return result;%0A        %7D%0A%0A        @Override%0A        public int compareTo(Dep o) %7B%0A            return this.number - o.number;%0A        %7D
 %0A   

refactor ArraySort class, add internal class and two comparator methods.