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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:00:48+00:00 2026-06-15T16:00:48+00:00

Please could someone suggest the best possible solution to the following problem (this is

  • 0

Please could someone suggest the best possible solution to the following problem (this is part of a program I am trying to code) :

I have a class Foo with member variables and another class Bar which extends Foo. Bar adds extra member variables. Bar objects are stored in a collection and as such I would like to order these objects by any of their member variables.

I considered using an enum type containing the member variables for the classes that could be passed as an argument to the compareTo() method but as enumerated types are implicitly static I cannot add parameters for the member variables for Bar or any other classes that may extend Foo that I want to compare.
For example, I want to compare two Bar classes by the member variable ‘myVar’ I would call something like bar1.compareTo(bar2, MyEnum.MYVAR) and the classes would be compared against their myVar values.
I am thinking that I may just have to create seperate methods to order by each member variable but want to use compareTo() as it is used by Java collections to automatically sort its items (I believe).

That is as clearly as I can describe the problem but I am probably going about achieving this the completely wrong way. Any pointers would be greatly appreciated.

EDIT – This is similar behaviour to sorting columns in a Windows Explorer window by different attributes i.e. Date Modified, File Name, size, etc.

  • 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-15T16:00:49+00:00Added an answer on June 15, 2026 at 4:00 pm

    You’re better off using a Comparator instead to encapsulate this particular comparation logic. You can do as many Comparator object as possible comparations you want to make or, as suggested, have the Comparator to compare based on a determined field.

    It’s up to you if you want to have multiple comparators or just one, but if you want to make complex comparations based on multiple fields I suggest you create a particular comparator for that.

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

Sidebar

Related Questions

Could someone please suggest why this is happening... I’ve got some code to pretty
Could someone please suggest where I might be going wrong with the following code?
Please could someone explain to me why this regex does not match anything, when
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer
Could someone please tell me what I am doing wrong here. I have compiled
Could someone please suggest a tool to create a Domain Model diagram? Some details:
Could someone please suggest a good way of taking a global seed value e.g.
I'm trying to develop a form of social network in Lift, could someone please

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.