Сортировка в файл

Нужно исправить код, чтобы он немного отличался, возможно ли сделать сортировку по другому принципу?

import java.util.*;
import java.io.*;

class CompStr implements Comparator
{
     public int compare(Object a, Object b) {
         String aStr, bStr;

         aStr = ((StrCont)a).get_first_segm();
         bStr = ((StrCont)b).get_first_segm();
         return aStr.compareTo(bStr);
    }
}

public class SortBox {
     ArrayList all_str;
     boolean sorted;
     CompStr comp;
     
     ArrayList get_all_str() {

        return all_str;
    }
     boolean get_sorted() {
        
        return sorted;
    }
 CompStr get_comp() {

    return comp;
}
 void set_all_str(ArrayList all_str) {

    this.all_str = all_str;
}
  void set_sorted(boolean sorted) {

   this.sorted = sorted;
}
 void set_comp(CompStr comp) {

    this.comp = comp;
}

SortBox()
{
    set_all_str(new ArrayList());
    set_sorted(true);
    set_comp(new CompStr());
}

boolean is_sorted()
{
    return get_sorted() == true;
}
void add(String str)
{
    get_all_str().add(new StrCont(str));
    set_sorted(false);
}
void read_file(String filePath) throws IOException {
    FileReader red;
    try
    {
        red = new FileReader(filePath);
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found.");
        return;
    }
    BufferedReader br = new BufferedReader(red);
    String line;
    while ((line = br.readLine()) != null)
    {
        add(line);
    }
    red.close();
}

void sort()
{
    Collections.sort(get_all_str(), get_comp());
    set_sorted(true);
}
void save_in_file(String filePath) throws IOException
{
    if (!is_sorted())
        sort();
    File file = new File(filePath);
    if (file.exists())
    {
        System.out.println("File already exists.");
        return;
    }
    PrintWriter pw = new PrintWriter(new FileWriter(file), true);
    Iterator itr = get_all_str().iterator();
    while(itr.hasNext())
    {
       StrCont elem = (StrCont)itr.next();
       pw.println(elem.get_full_str());
       //System.out.print(elem.get_full_str());
    }
    //get_all_str().clear();
}

}


Ответы (0 шт):