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

如何在java中按字母顺序对字符串列表排序?,java,Java,嗨 我是Java初学者。我需要按字母顺序排列一串名字。 我有一个类,它从一个文本文件读取并写入排序文件,该文件按年龄(小于18岁)过滤,但我需要它按字母顺序过滤,下面是我的实现。它的工作没有名字过滤器 public class PatientFileProcessor { public void process(File source, File target) { System.out.println("source"

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

            }

我该怎么做呢?

假设这些是字符串,使用方便的静态方法


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

您可以在
患者
中实现
Comparable
接口,并覆盖
compareTo
方法。这样,当调用集合的排序方法时,它将使用您的compareTo方法进行比较。

您不能“按字母顺序筛选”,您必须按字母顺序排序。您要求对字符串列表进行排序,但代码中没有字符串列表。请澄清你的意思。是的Stephen你说得对,我的错误是一个对象列表我的患者对象如下:公共类患者{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;}等等sort()它抱怨无法使用void方法(排序)@Pami it说它不能fnd方法排序?很可能,你需要实现Comparable或类似的东西才能让它工作。你知道如何使用接口吗?另外:不要在注释中发布整个类的代码,字符串是不可访问的,是的,但它们不是字符串。你可以试试这个:patientList.stream().sorted((object1,object2)->object1.getName().compareTo(object2.getName());这是我创建的方法::public ListSortPatients(ListmYPatients){ListpatientList=new ArrayList();Collections.sort(patientList,Comparator.Comparating(Patient::getName));return patientList;}但我不知道该放在哪里这是我的写入方法,但它不会将patientList作为参数::public void writefile(文件目标,列表排序,列表patientList){BufferedWriter pw=new BufferedWriter(new FileWriter(target));DateFormat df=new SimpleDateFormat(“yyy/dd/MM”);例如(迭代器it=sorted.Iterator();it.hasNext();{Patient p=it.next();pw.append(p.getName()+“|”+p.getGender()+“|”+df.format(p.getDob());pw.newLine();}}添加此方法:public List sortByName(List ps){ps.sort(Comparator.Comparator)(Patient::getName,Comparator.comparing((字符串s)->!s.equals(“None”))。然后比较(Comparator.naturalOrder());返回ps;}并从process方法调用上述方法:public void process(文件源,文件目标){System.out.println(“source”+source.getAbsolutePath());System.out.println(“target”+target.getAbsolutePath()));尝试{writefile(target,sortByName(filterbyAge(readfile(source)));}catch(Exception ex){}
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;
    }
}
 java.util.Collections.sort(patients)