Java 我想用类中定义的变量按字母顺序排列对象

Java 我想用类中定义的变量按字母顺序排列对象,java,object,arraylist,alphabetized,Java,Object,Arraylist,Alphabetized,我正在制作一个程序,为我设置一个实验,我想按字母顺序排列我输入的主题(或人)。我有一个类型主题的数组列表,我想按它们的名字按字母顺序排列 import java.util.Random; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class Experiment { public Random number; public ArrayList<Str

我正在制作一个程序,为我设置一个实验,我想按字母顺序排列我输入的主题(或人)。我有一个类型主题的数组列表,我想按它们的名字按字母顺序排列

import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Experiment
{
public Random number;
public ArrayList<String> allSubject;
public ArrayList<Subject> allSubjects,alphaSubjects;
public ArrayList<Group> experiment;
public Integer value;
public HashMap<Integer,Subject> matched;
private ArrayList<Integer> numbers;
/**
 * Make a new Experiment.  Then use method addSubject to add
 * Subjects to your experiment.  Then call the assignGroups
 * method to assign Subjects to each group.
 */
public Experiment()
{
    number = new Random();
    numbers = new ArrayList<Integer>();
    experiment = new ArrayList<Group>();
    matched = new HashMap<Integer,Subject>();
    allSubjects = new ArrayList<Subject>(); 
    allSubject = new ArrayList<String>();
    alphaSubjects = new ArrayList<Subject>();
}

/**
 * Alphabetizes the list of Subjects based on their
 * name input by the user.  As of right now, this method
 * is case sensitive meaning Strings starting with 
 * capitals will be listed before those without capitals.
 */
private void alphabetize()
{

           Collections.sort(allSubject);

        //compare the String arraylist to the subject arraylist to reset the subject arraylist indeces in alphabetical order.

       for(int i =0;i<allSubject.size();i++)
       {
        String theName = allSubject.get(i);
         for(Subject subject:allSubjects)
        {
          if(subject.getName().toLowerCase().contains(theName))
         {
            alphaSubjects.add(new Subject(subject.getName(),subject.getDescription()));
         }
        }

     }
}
/**
 * Adds a new Subject to the experiment.
 */
public void addSubject(String name, String description)
{
    allSubjects.add(new Subject(name,description));
    allSubject.add((name.toLowerCase()));
}

}

您可以根据自己的需要实现Comparator或Comparable并覆盖compare(..)和compareTo(..)方法。在这种情况下,你需要在实施这个方法时考虑“主题的名称”。然后,
Collections.sort(yourList)
将根据主题名称为您提供排序结果

    import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class Subject implements Comparable<Subject>
{
public final String name;
public final String description;
public Subject(String name, String description)
{
    this.name = name;
    this.description = description;
}
public Subject(int aNumber)
{
    name = "Subject" + aNumber;
    aNumber++;
    description = "default";
}
public String getName()
{
    return name;
}
public String getDescription()
{
    return description;
}

    @Override
    public int compareTo(Subject o) {
        return this.getName().toUpperCase().compareTo(((Subject)o).getName());
    }

    public static void main(String[] args) {
        Subject s3 = new Subject("C","");
        Subject s1 = new Subject("Z","");
        Subject s2 = new Subject("A","");

        List<Subject> list = new ArrayList<Subject>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        Collections.sort(list);

        for(Subject sub:list){
            System.out.println(sub.getName());
        }
    }
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共课科目实行可比性
{
公共最终字符串名;
公共最终字符串描述;
公共主题(字符串名称、字符串描述)
{
this.name=名称;
this.description=描述;
}
公共主题(国际数量)
{
name=“Subject”+数量;
a数++;
description=“default”;
}
公共字符串getName()
{
返回名称;
}
公共字符串getDescription()
{
返回说明;
}
@凌驾
公共内部比较(主题o){
返回这个.getName().toUpperCase().compareTo(((Subject)o.getName());
}
公共静态void main(字符串[]args){
主题s3=新主题(“C”和“);
受试者s1=新受试者(“Z”和“Z”);
受试者s2=新受试者(“A”和“);
列表=新的ArrayList();
列表。添加(s1);
列表。添加(s2);
增加(s3);
集合。排序(列表);
用于(主题子:列表){
System.out.println(sub.getName());
}
}
}

您所需要做的就是让类实现
compariable
并添加
comparieto()

公共类主题实现可比较
{
....
@凌驾
公共内部比较(主题其他)
{
返回这个.getName().compareTo(other.getName());
}
}
现在,由于类实现了自身的可比较性,因此可以使用
Collections.sort()
主题列表进行排序


这里有一个关于更多信息的链接。祝你好运

您可以使用自己的比较器使用SortedList()包装主题ArrayList

SortedList sortedSubjects = new SortedList<Subject>(allSubjects,new Comparator<Subject>() {
        @Override
        public int compare(Subject left, Subject right) {
            return left.getName().compareTo(right.getName);
        }
    }); 
SortedList sortedSubjects=新的SortedList(所有受试者,新的比较器(){
@凌驾
公共整数比较(主题左、主题右){
返回left.getName().compareTo(right.getName);
}
}); 

很抱歉,您能给我一个参考,让我学习如何编写自己的比较器吗?我正在学习java。@Mr.Meeshmew看到我的答案。@Mr.Meeshmew希望这能回答您的疑问。
public class Subject implements Comparable<Subject>
{
    ....

     @Override
     public int compareTo(Subject other)
     {
         return this.getName().compareTo(other.getName());
     }
}
SortedList sortedSubjects = new SortedList<Subject>(allSubjects,new Comparator<Subject>() {
        @Override
        public int compare(Subject left, Subject right) {
            return left.getName().compareTo(right.getName);
        }
    });