简体   繁体   English

如何在Java中按名称对ArrayList值进行排序

[英]How to sort ArrayList values by name in java

I am getting student information from database, 我正在从数据库中获取学生信息,

ArrayList<Student> studentList = session.createQuery("from Student order by Date").list();

the studentList contains name , id ,marks, by date. studentList包含按日期显示的名称,id,标记。 I want to display this arraylist by name, becase the same student name contains different date. 我想按名称显示此arraylist,因为同一名学生的姓名包含不同的日期。 How to sort this from arraylist. 如何从arraylist排序。 Ex studentList value is 以前的StudentList值为

1 x  2010-10-01
2 y  2010-10-05
3 z  2010-10-15
1 x  2010-10-10
1 x  2010-10-17
2 y  2010-10-15
4 xx 2010-10-10

I want to display this to 我想显示这个

1 x  2010-10-01
1 x  2010-10-10
1 x  2010-10-17
2 y  2010-10-05
2 y  2010-10-15
3 z  2010-10-15
4 xx 2010-10-10

and store this to another array list 并将其存储到另一个数组列表

There are plenty of questions to look at that answer this, such as: https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property 有很多问题需要解决,例如: https : //stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property

But here is an example program of what to do. 但是这里是一个示例程序。 I assumed you wanted to sort by name first, and then date. 我假设您想先按名称排序,然后按日期排序。 You can put logic to do that in the custom comparator. 您可以在自定义比较器中添加逻辑来做到这一点。

import java.util.*;

public class SortExample {

  public static class Student {
    public String name;
    public String date;

    public Student(String name, String date) {
      this.name = name;
      this.date = date;
    }
  }

  public static class StudentComparator implements Comparator<Student> {
      @Override
      public int compare(Student s, Student t) {
         int f = s.name.compareTo(t.name);
         return (f != 0) ? f : s.date.compareTo(t.date);
      }
  }

  public static void main(String args[]) {
    ArrayList<Student> l = new ArrayList<Student>(Arrays.asList(
      new Student ("x","2010-10-5"),
      new Student ("z","2010-10-15"),
      new Student ("y","2010-10-05"),
      new Student ("x","2010-10-1")
    ));

    System.out.println("Unsorted");
    for(Student s : l) {
      System.out.println(s.name + " " + s.date);
    }

    Collections.sort(l, new StudentComparator());

    System.out.println("Sorted");
    for(Student s : l) {
      System.out.println(s.name + " " + s.date);
    }
  }
}

Output of this is: 输出为:

Unsorted
x 2010-10-5
z 2010-10-15
y 2010-10-05
x 2010-10-1
Sorted
x 2010-10-1
x 2010-10-5
y 2010-10-05
z 2010-10-15

EDIT : This sorts the array list in place. 编辑 :这对数组列表进行排序。 You'd have to copy it first if you want it as a new list. 如果要将其作为新列表,则必须先复制它。

The methods you require are: 您需要的方法是:

Collections.sort(List<T>)

Collections.sort(List<T>, Comparator<? super T>)

The first method can be used if your Student class implements the Comparable interface. 如果您的Student类实现Comparable接口,则可以使用第一种方法。 As a side note it's worth considering whether in fact your data should be stored in a sorted data structure such as a SortedMap (eg TreeMap implementation). 附带说明一下,值得考虑的是,实际上您的数据是否应该存储在排序的数据结构中,例如SortedMap (例如TreeMap实现)。

I've written a framework to sort natural language text representations of objects in locale-sensitive order: 我编写了一个框架,以对语言环境敏感的顺序对对象的自然语言文本表示进行排序:

http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizables.html http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/text/Localizables.html

Either Student would need to implement Localizable or you would have to provide a StudentLocalizer by extending Localizer . 学生将需要实现Localizable,或者您必须通过扩展Localizer提供一个StudentLocalizer。

Maven: Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>lib-core</artifactId>  
    <version>0.1</version>  
</dependency>  

Download: 下载:

http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1/ http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.1/

A test implementation could be: 测试实现可以​​是:

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

public class Execute {

    public static void main(String args[]) {
        List<Student> list = new ArrayList<Student>();

        list.add(new Student("TestX"));
        list.add(new Student("TestA"));

        System.out.println(list);

        Collections.sort(list);

        System.out.println(list);
    }

}

class Student implements Comparable<Student> {

    private String name;

    public Student() {
        super();
    }

    public Student(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public int compareTo(Student student) {
            if (this.name == null || student.name == null) {
                return 0;
            }
        return name.compareTo(student.name);
    }

    @Override
    public String toString() {
        return name;
    }

}

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

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