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

  • Home
  • SEARCH
  • 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 8309285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:04:17+00:00 2026-06-08T19:04:17+00:00

I have a base class which includes the following line to sort my objects

  • 0

I have a base class which includes the following line to sort my objects ArrayList:

Collections.sort(objects, SortObjectList);

It also includes the following class definition to define the sort:

private class SortObjectList implements Comparator<T>
{
    public int compare(T lhs, T rhs) 
    {
        return lhs.name.compareToIgnoreCase(rhs.name);
    }
}

In order to allow a derived class to sort using different criteria, I have implemented the following:

Collections.sort(objects, getSortObjectClass());

and

public Comparator<T> getSortObjectClass() {
    return new SortObjectList();
}
private class SortObjectList implements Comparator<T>
{
    public int compare(T lhs, T rhs) 
    {
        return lhs.name.compareToIgnoreCase(rhs.name);
    }
}

I then override the sort in the derived class as follows:

@Override
public Comparator<MyDataClass> getSortObjectClass() {
    return new SortObjectList();
}
private class SortObjectList implements Comparator<MyDataClass>
{
    public int compare(MyDataClasslhs, MyDataClassrhs) 
    {
        return lhs.seqNo - rhs.seqNo;
    }
}

Which orders by seqNo rather than name.

It works, but seems a bit clunky. Does anyone know of a more elegant solution?

  • 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-08T19:04:18+00:00Added an answer on June 8, 2026 at 7:04 pm

    Well you can make it slightly more compact using an anonymous inner class:

    @Override
    public Comparator<MyDataClass> getSortObjectClass() {
        return new Comparator<MyDataClass> {
            @Override
            public int compare(MyDataClass lhs, MyDataClass rhs)  {
                return lhs.seqNo - rhs.seqNo;
            }
        };
    }
    

    But that’s about as compact as it gets in Java.

    (Note that your comparison above will give the wrong result on overflow, by the way.)

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

Sidebar

Related Questions

I want to have a base class which dictates the alignment of the objects
I define a base class which have a Long primary key , just like
Okay I have a series of objects based on a base class which are
I have a base class, which includes all other files. I can access this
Ok, so I have a base class which declares the event StatusTextChanged . My
I have a Exception base class which defines a stream function: class Exception {
I have a class which is derived from a base class, and have many
I have written a base class from which I wish to derive several child
I have 2 web user controls, both inherit the same base class which extends
In my model, I have a base object class A which holds a set

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.