简体   繁体   English

Java-按字母和数字顺序对带有对象的数组列表进行排序

[英]Java - Sort Array List with Objects Alphabetically and Numerically

Im reading Classroom names ("B106") from a text file and saving them on a Array list. 我从文本文件中读取教室名称(“ B106”),并将其保存在数组列表中。 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? 我读到一些有关使用Collections / Comparator的信息,但是我无法成功实现它们,该怎么办?

Ex: Input: "B106", "D111", "A201", "B102" 例如:输入:“ B106”,“ D111”,“ A201”,“ B102”

Output: "A201", "B102", "B106", "D111" 输出:“ A201”,“ B102”,“ B106”,“ D111”

Class Core_Salas where I manage my files 我在Core_Salas类中管理文件

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 具有getter设置程序和构造函数的Sala类

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;
}
}

Collections.sort() method can help you. Collections.sort()方法可以为您提供帮助。

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);
  }
}

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 请让我知道这对你有没有用

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: 我认为,如果您的Sala类仅具有一个String类型的属性,则无需制作Sala类。您可以将String直接添加到arrayList.and使用Collections.sort(),然后它将按字母顺序和数字顺序进行排序。例如:

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. 但是如果必须使用Sala类,则可以实现Comparator或Comparable Interface。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM