1

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

lakshmi
  • 4,539
  • 17
  • 47
  • 55
  • 9
    If you want it sorted by name, then why do you query for it sorted by date? Sorting it in the DB is **definitely** the better approach: `"from Student order by name, date"` should do it. – Joachim Sauer Oct 20 '11 at 09:15

4 Answers4

6

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.

Community
  • 1
  • 1
Benedict
  • 2,771
  • 20
  • 21
5

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 (e.g. TreeMap implementation).

Adamski
  • 54,009
  • 15
  • 113
  • 152
1

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/

Puce
  • 37,247
  • 13
  • 80
  • 152
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;
    }

}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106