1

Im reading Classroom names ("B106") from a text file and saving them on a Array list. I need to sort that Array by letter and then by number. I read something about using Collections / Comparator but I wasnt able to implement them sucessfully, how can I do this?

Ex: Input: "B106", "D111", "A201", "B102"

Output: "A201", "B102", "B106", "D111"

Class Core_Salas where I manage my files

public class Core_Sala {

//---------------------------------------------------------------------------
public static ArrayList<Sala> Sala = new ArrayList<Sala>();
private static File SF = new File("Salas.txt");
public static void LerSalas()
{

    try
    {
        if(SF.exists()==true)
        {
            String[]parts;
            Sala.clear();//Array list limpar
            Scanner inputFile = new Scanner(SF);
            while (inputFile.hasNextLine()) {
                String line = inputFile.nextLine();
                Sala s1 = new Sala(line);
                Sala.add(s1);
            }
            inputFile.close();
        }
    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
    if(SF.exists()==false)
    {
        PrintWriter out = null;
        try
        {
            out = new PrintWriter(SF);

        }catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "O ficheiro de utilizadores estava danificado ou era inexistente, foi criado um novo com o utilizador admin!");
        }
        out.close();
    }
}
}

Class Sala with getter Setter and Constructor

public class Sala {
private String Salas;

public Sala(String salas)
{
    this.Salas=salas;
}
public String getSalas() {
    return Salas;
}
public void setSalas(String salas) {
    Salas = salas;
}

@Override
public String toString()
{
    return Salas;
}
}
3
  • If all strings follow that same pattern, you can just sort alphabetically. No need to parse the string. Commented Jun 8, 2016 at 0:01
  • Will only sort alphabetically also get it right with the numbers? Also I will try Seek Addo link seems quite simple, although I dont know if I'm allowed to use that code, since this is a project for school and the tracher never talked about it. . . I'll try my luck thank you Commented Jun 8, 2016 at 0:08
  • 1
    It will work if the numbers are of the same length (or padded). So A123 will work with A099 but not with A77. Commented Jun 8, 2016 at 1:39

3 Answers 3

1

You can try this

 public static void main(String[] args) {
    LerSalas();
    Collections.sort(salaData, new Comparator<Sala>() {
        @Override
        public int compare(Sala o1, Sala o2) {
            return o1.getSalas().compareTo(o2.getSalas());
        }
    });
    salaData.stream().forEach(System.out::println);
}

let me know if it works for you

Sign up to request clarification or add additional context in comments.

Comments

1

Collections.sort() method can help you.

Example:

public class TestSort {
  public static void main(String args[]) {
    String init[] = {"B106", "D111", "A201", "B102"};
    List list = new ArrayList(Arrays.asList(init));
    System.out.println("Input: " + list);
    Collections.sort(list);
    System.out.println("Output: " + list);
  }
}

Comments

0

For my opinion,if your Sala Class has only one attribute that is String type,Then you no need to make Sala Class.You can add String directly to arrayList.and Use Collections.sort(),then it will sort alphabatically and numerically.For eg:

public class Test {

    public static void main(String args[]){
        List al = new ArrayList();
        al.add("B106");al.add("A201");al.add("D111");al.add("BC102");
        Collections.sort(al);
        System.out.println(al);
    }
}

but if you have to use Sala class,then you can implements Comparator or Comparable Interface.

1 Comment

This is a project for school im forced to create a class for anything that I will have to manage with the program, I have no choice =/, but thanks for the reply anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.