package ru.matevosyan;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created SortFile for sort big file and write it to a new file.
 * Created on 19.02.2017.
 * @since 1.0
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class SortFile {

    public void sort(File source, File dist) throws IOException{


        try (RandomAccessFile toRead = new RandomAccessFile(source, "r");
             RandomAccessFile toWrite = new RandomAccessFile(dist, "rw")) {
            int stringSize = 0;
            while (toRead.readLine() != null) {
                stringSize++;
            }

            String[] stringsFirstToRead = new String[stringSize / 2];
            String[] stringsSecondToRead;
            if (stringSize % 2 == 0) {
                stringsSecondToRead = new String[stringSize / 2];
            } else {
                stringsSecondToRead = new String[stringSize / 2 + 1];
            }
            String[] stringsThirdSortedToRead;
            toRead.seek(0);
                for (int b = 0; b < stringsFirstToRead.length; b++) {
                    stringsFirstToRead[b] = toRead.readLine();
                    if (b >= stringsFirstToRead.length -1) {
                        for (int c = 0; c < stringsSecondToRead.length; c++) {
                            stringsSecondToRead[c] = toRead.readLine();
                        }
                    }
                }

            stringsThirdSortedToRead = sortArray(sortOneArray(stringsFirstToRead), sortOneArray(stringsSecondToRead));

           for (int i = 0; i < stringsThirdSortedToRead.length; i++) {
                String aStringsThirdSortedToRead = stringsThirdSortedToRead[i];

                toWrite.write(aStringsThirdSortedToRead.getBytes());
               String s = System.getProperty("line.separator");
                toWrite.write(s.getBytes());
            }


            } catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }


    }

    public String[] sortArray(String[] firstStrings, String[] secondStrings) {

            int j = 0;
            int i = 0;
            int k = 0;
        int count = firstStrings.length + secondStrings.length;
        int countForI = 0;
        int countForJ = 0;
            int length = firstStrings.length + secondStrings.length;
            String[] stringsThirdToRead = new String[length];

            while (count != 0) {

                    if (firstStrings[i].length() <= secondStrings[j].length()) {


                        if (countForI != firstStrings.length) {
                            stringsThirdToRead[k] = firstStrings[i];
                            countForI++;
                            if (i < firstStrings.length - 1) {
                                i++;
                            }

                        } else {
                            if (countForJ != firstStrings.length) {
                                stringsThirdToRead[k] = secondStrings[j];
                                countForJ++;
                            }
                            if (j < secondStrings.length - 1) {
                                j++;

                            }
                        }

                    } else {

                        if (j < secondStrings.length) {
                            stringsThirdToRead[k] = secondStrings[j];
                        }

                        if (j < secondStrings.length - 1) {
                            j++;

                        }
                    }

                if (k < (length - 1)) {
                    k++;
                }

                count--;
            }

            return stringsThirdToRead;
        }

        public String[] sortOneArray(String[] array) {
            for (int out = 0; out < array.length; out++) {
                for(int in = array.length - 1; in > out; in--) {


                    if (array[in].length() < array[in - 1].length()) {
                        String tmp = array[in];
                        array[in] = array[in - 1];
                        array[in - 1] = tmp;

                    }

                }
            }
            return array;
        }
    }

@@ -281,24 +281,755 @@
 SortFile %7B%0A%0A
+    /**%0A     * array stringsThirdSortedToRead that write to the file in %7B@link #sort(File, File)%7D method.%0A     */%0A%0A    private String%5B%5D stringsThirdSortedToRead;%0A%0A    /**%0A     * for get sorted array.%0A     * @return stringsThirdSortedToRead an array that hold sorted strings by string length.%0A     */%0A%0A    public String%5B%5D getStringsThirdSortedToRead() %7B%0A        return stringsThirdSortedToRead;%0A    %7D%0A%0A    /**%0A     * get two files sort that passing first and write sorted array to file that passing second.%0A     * @param source original file that exist and that must be sorted.%0A     * @param dist is the file that using to write sorted array.%0A     * @throws IOException that can be throw when RandomAccessFile was created.%0A     */%0A%0A
     public v
@@ -1079,18 +1079,18 @@
 xception
+ 
 %7B
-%0A
 %0A%0A      
@@ -1684,54 +1684,8 @@
   %7D%0A
-            String%5B%5D stringsThirdSortedToRead;
 %0A   
@@ -1898,16 +1898,17 @@
 length -
+ 
 1) %7B%0A   
@@ -2583,25 +2583,24 @@
          %7D%0A%0A
-%0A
             
@@ -2643,63 +2643,433 @@
     
-System.out.println(ioe.getMessage());%0A        %7D%0A%0A%0A
+ioe.getMessage();%0A            %7D%0A%0A%0A    %7D%0A%0A    /**%0A     * Sorted two passing sorted array that merge in one sorted array that was filling in %7B@link #sort(File, File)%7D.%0A     * @param firstStrings sorted array that going to be merged with the second passing array.%0A     * @param secondStrings sorted array that going to be merged with the first passing array.%0A     * @return merged array %7B@link #stringsThirdSortedToRead%7D%0A 
     
-%7D
+*/
 %0A%0A  
@@ -4796,24 +4796,202 @@
      %7D%0A%0A    
+/**%0A     * Method %7B@link #sortOneArray(String%5B%5D)%7D that sorting array.%0A     * @param array tha passing for sorted by string length.%0A     * @return array that was sorted.%0A     */%0A%0A
     public S
@@ -5107,16 +5107,17 @@
      for
+ 
 (int in 
@@ -5151,25 +5151,24 @@
 t; in--) %7B%0A%0A
-%0A
             

add javadoc