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 8737217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:28:16+00:00 2026-06-13T10:28:16+00:00

Possible Duplicate: Sorting an ArrayList of Contacts based on name? I have a student

  • 0

Possible Duplicate:
Sorting an ArrayList of Contacts based on name?

I have a student object and then I create ArrayList and add student to the list.

ArrayList<Student_Object> studentsList = new ArrayList<>();

Now, I want to sort the list by studentId fleid. How can I do it?

Is there a better solution? Thanks


So I have this method in the Student _Object class

Class is:

class Student_Object implements Comparator<Student_Object>

The method is:

public int compare(Student_Object student1, Student_Object student2){
    String student1TUID = student1.getTUID();        
    String student2TUID = student2.getTUID();

return student1TUID.compareTo(student2TUID);   


}

From where do I run the statment?

Collections.sort(studentsList);

If I run it from my main class I get error in netbeans:

no suitable method found for sort(ArrayList<Student_Object>)
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
    method Collections.<T#2>sort(List<T#2>) is not applicable
      (inferred type does not conform to declared bound(s)
        inferred: Student_Object
        bound(s): Comparable<? super Student_Object>)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
    T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
----
(Alt-Enter shows hints)

Got it to work. I used Collections.sort(studentsList, new Student_Object());

Thanks everyone

  • 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-06-13T10:28:17+00:00Added an answer on June 13, 2026 at 10:28 am

    One way would be:

    Write a comparator and override compare method. Then use Collections.sort() by passing comparator.

    Example:

    class StudentComparator implements Comparator<Student> {
    
        public int compare(Student stud1, Student stud2){
    
            int stu1ID = stud1.getId();       
            int stu2ID = stud2.getId();
    
            if(stu1ID > stu2ID)
                return 1;
            else if(stu1ID < st21ID )
                return -1;
            else
                return 0;    
        }
    
    }
    

    Another flavor may be:

     class StudentComparator implements Comparator<Student> {
    
            public int compare(Student stud1, Student stud2){
    
                int stu1ID = stud1.getId();       
                int stu2ID = stud2.getId();
    
               return stud1ID-stu2ID;
            }
    
        }
    

    This tutorial may help you.

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

Sidebar

Related Questions

Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
Possible Duplicate: Ignoring 'A' and 'The' when sorting with XSLT I have a list
Possible Duplicate: Sorting by Name (alphabetic) Say I have the code below. I have
Possible Duplicate: Sorting an ArrayList of Contacts I am storing DataNode objects in an
Possible Duplicate: Sorting an IList in C# I have an IList<Keyword> where Keyword is
Possible Duplicate: Sorting a hash in Ruby by its value first then its key.
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: Sorting a collection of objects How to sort the list in java
Possible Duplicate: ArrayList Vs LinkedList ArrayList vs. LinkedList which one is better for sorting
Possible Duplicate: Sorting list of lists by a third list of specified non-sorted order

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.