简体   繁体   中英

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. I want to display this arraylist by name, becase the same student name contains different date. How to sort this from arraylist. Ex studentList value is

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

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. 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).

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

Either Student would need to implement Localizable or you would have to provide a StudentLocalizer by extending Localizer .

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/

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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