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.split("\\\\");
                String[] s2 = o2.split("\\\\");
                int result;
                if (s1.length == s2.length) {
                    result = o2.compareTo(o1);
                }
                else {
                    result = s2[0].compareTo(s1[0]);
                }
                return result;
            });
        }
        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;
    }
}

@@ -746,18 +746,16 @@
   String
-%5B%5D
  s1 = o1
@@ -755,27 +755,38 @@
 s1 = o1.
-split(%22%5C%5C%5C%5C
+replaceAll(%22%5B%5E0-9%5D%22, %22
 %22);%0A    
@@ -803,18 +803,16 @@
   String
-%5B%5D
  s2 = o2
@@ -816,71 +816,55 @@
  o2.
-split(%22%5C%5C%5C%5C%22);%0A                int result;%0A                if (
+replaceAll(%22%5B%5E0-9%5D%22, %22%22);%0A                s1 = 
 s1.l
@@ -872,213 +872,214 @@
 ngth
+()
  == 
-s2.length) %7B%0A                    result = o2.compareTo(o1);%0A                %7D%0A                else %7B%0A                    result = s2%5B0%5D.compareTo(s1%5B0%5D);%0A                %7D%0A                return result
+1 ? s1 + %2299%22 : s1.length() == 2 ? s1 + %229%22 : s1;%0A                s2 = s2.length() == 1 ? s2 + %2299%22 : s2.length() == 2 ? s2 + %229%22 : s2;%0A                return Integer.valueOf(s2) - Integer.valueOf(s1)
 ;%0A  
@@ -1777,9 +1777,8 @@
 %0A    %7D%0A%7D
-%0A

refactor ArraySort class, make without additional internal class. some kind of crutch....