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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:35:48+00:00 2026-05-29T15:35:48+00:00

I created a class called Foo. Foo has three fields, which are all ints:

  • 0

I created a class called Foo. Foo has three fields, which are all ints: x, y, and z. I want to make a PriorityQueue<Foo> which prioritizes the Foo objects differently in different situations. For instance, I might want to prioritize by x value, or maybe by y value, or maybe by z. But I won’t know which value I want to prioritize by until runtime. I’ve heard you can use comparators to somehow impose an ordering on the fly, which I think would be perfect here.

I’m confused as to exactly how I would do this though. Could someone please show me an example if say I wanted to prioritize on x using a comparator (without having to override the compareTo function in my Foo class)?

Thank you very much.

  • 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-29T15:35:50+00:00Added an answer on May 29, 2026 at 3:35 pm

    A comparator is a parameterized interface that allows you to define how two instances of the parameterized type can be compared. let’s assume you have the following class

    class Foo {
       int x;
       int y;
       int z;
    }
    

    Then to define a comparator that orders elements based on their x value then y then z we’d do the following

    class XyzComparator implements Comparator<Foo> {
        @Override
        public int compare(Foo foo1, Foo foo2) {
              if(foo1.x != foo2.x) {
                   return Integer.compare(foo1.x, foo2.x);
              }
              if(foo1.y != foo2.y) {
                   return Integer.compare(foo1.y, foo2.y);
              }
              return Integer.compare(foo1.z, foo2.z);
        }
    }
    

    Similarly you can define comprators that compare elements based first on their y value then x the z…etc
    Finally at runtime you can instantiate a PriorityQueue with that comparator

    PriorityQueue<Foo> queue;
    if(x_then_y_then_z) {
        queue = new PriorityQueue<Foo>(10, new XyzComparator());
    } else if (y_then_x_then_z) {
        queue = new PriorityQueue<Foo>(10, new ZxyComparator());
    }
    

    For more information take a look at the priority queue javadoc as well as the comparator javadoc

    Edit: Please see @buritos comment regarding integer overflow.

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

Sidebar

Related Questions

My situation is essentially this: I have a class called Foo which has dependencies
I have created a class library called AddServiceLibrary in which I have a method
I have object called Foo. Right now it implements IFoo which has a lot
I want to create user-specific validations. User has a column called rule_values which is
I am debugging some C# class (let's say, Foo) which has a Dispose-Finalize pattern
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have a peculiar problem. I created a normal Objective C class called SampleTable.
I'm trying to create a point class which defines a property called coordinate. However,
The following Groovy code creates a GORM-persisted domain class called Foo when written to
Let's say I have a class in my web app called class Foo. It

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.