如何在Java中按字母顺序对字符串列表进行排序?

时间:2018-10-03 12:04:57

标签: java

嗨。

我是Java初学者。我需要按字母顺序对名称字符串进行排序。 我有一个从文本文件读取并写入按年龄(小于18)过滤的排序文件的类,但是我需要它按字母顺序过滤,以下是我的实现。无需按名称过滤即可正常工作。

        public class PatientFileProcessor {

            public void process(File source, File target) {

                System.out.println("source"+source.getAbsolutePath());
                System.out.println("target"+target.getAbsolutePath()); 

                try {
                    writefile(target, filterbyAge(readfile(source)));
                } catch (Exception ex) {
                    Logger.getLogger(PatientFileProcessor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public List<Patient> readfile(File source) throws Exception {
                List<Patient> patients = new ArrayList();
                BufferedReader bf = new BufferedReader(new FileReader(source));
                String s = bf.readLine();// ignore first line
                while ((s = bf.readLine()) != null) {
                    String[] split = s.split("\\|");
                    System.out.println(Arrays.toString(split));
                    System.out.println(s+"       "+split[0]+"       "+split[1]+"       "+split[2]);
                    Date d = new SimpleDateFormat("yyyy-dd-MM").parse(split[2]);
                    patients.add(new Patient(split[0], split[1], d));
                }
                return patients;
            }

            public void writefile(File target, List<Patient> sorted) throws Exception {

                BufferedWriter pw = new BufferedWriter(new FileWriter(target));
                DateFormat df = new SimpleDateFormat("yyyy/dd/MM");

                for (Iterator<Patient> it = sorted.iterator(); it.hasNext();) {
                    Patient p = it.next();


                    pw.append(p.getName() + "|" + p.getGender() + "|" + df.format(p.getDob()));
                    pw.newLine();
                }
                pw.flush();
            }

            public List<Patient> filterbyAge(List<Patient> ps) {
                List<Patient> sorted = new ArrayList();
                for (Iterator<Patient> it = ps.iterator(); it.hasNext();) {
                    Patient s = it.next();
                    if (calcAge(s.getDob()) > 18) {
                        sorted.add(s);
                    }

                }
                return sorted;
            }


            public int calcAge(Date d) {
                Date today = new Date();
                long m = today.getTime() - d.getTime();
                return (int) (m / (1000 * 60 * 60 * 24 * 365.25));
            }

            }

患者:

import java.util.Date;

public class Patient {
    private String name;
    private String gender;
    private Date dob;

    public Patient() {
    }

    public Patient(String name, String gender, Date dob) {
        this.name = name;
        this.gender = gender;
        this.dob = dob;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDob() {
        return dob;
    }
}

我该怎么办?

3 个答案:

答案 0 :(得分:1)

假设它们是字符串,请使用方便的静态方法sort

 java.util.Collections.sort(patients)

答案 1 :(得分:0)

对于字符串,这将起作用: arrayList.sort((p1,p2)-> p1.compareTo(p2)); (Java 8)

答案 2 :(得分:0)

您可以做的是可以在Comparable中实现Patient接口,并覆盖compareTo方法。这样,调用Collections的sort方法时,它将使用您的compareTo方法进行比较。