Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:32+00:00 2026-05-26T08:25:32+00:00

I am getting student information from database, ArrayList<Student> studentList = session.createQuery(from Student order by

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:25:32+00:00Added an answer on May 26, 2026 at 8:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Few days back, I posted a question about getting student marks results from database
I created an arraylist of Student type. Student has name, subject information in it.
I'm getting strings like this from HttpWebRequest: Student■John■Smith ■Subject1■10■10 ■■Exam■A■B■C ■■Questions■B■A■C ■Subject2■223■227 Student■Ala■Cat ■Subject3■101■102
I am using SQL query to pull some student records from SQL database. I
I am a student with a mainly electronics background getting into programing. The more
Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
I am a college student getting my Computer Science degree. A lot of my
I am a student this is homework. I'm getting tired and confused. Any advice
I am having difficulties getting a subset of results From a huge set of
I'm having trouble getting the results to show up right, I want a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.