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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:51:30+00:00 2026-05-31T16:51:30+00:00

I have an object, Test , that has two properties, double x and double

  • 0

I have an object, Test, that has two properties, double x and double y. I want to add these objects to a SortedSet, keeping the set sorted in ASC order on x of Test. If two instances of Test have the same x values, I want them to be sorted within the set by their y values.

I thought the following would do the trick:

private SortedSet<Test> tests = new TreeSet<Test>(new Comparator<Test>() {

@Override
public int compare(Test o1, Test o2) {
    if (o1.getXpos() < o2.getXpos()) {
        return -1;
    }
    if (o1.getXpos() > o2.getXpos()) {
        return 1;
    }
    if (o1.getXpos() == o2.getXpos()) {
        if (o1.getYpos() < o2.getYpos()) {
            return -1;
        }
        if (o1.getYpos() > o2.getYpos()) {
            return 1;
        }
        if (o1.getYpos() == o2.getYpos()) {
            return 0;
        }
    }
    return 0;
}
});

Instead this orders the actual x and y values; i.e.

testA: x=200, y=200,

testB: x=200, y=400

After inserting into tests:

testA: x=200, y=200,

testB: x=400, y=200

Instead of the instances within tests.

  • 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-31T16:51:32+00:00Added an answer on May 31, 2026 at 4:51 pm

    your comparator is correct. you’ve got bigger problems, though, if adding your Test objects to the set changes their member variables, e.g. “testB: x=400, y=200” -> “testB: x=200, y=400”. I would guess your problem lies in code you have not included (maybe a botched constructor?).

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

Sidebar

Related Questions

I have mocks working where I test that methods on my mocked object are
I have a little JUnit-Test that export an Object to the FileSystem. In the
I have an object that needs a test if the object data is valid.
If I have a reference to an object: var test = {}; that will
I am using C# 2.0 with Nunit Test. I have some object that needs
I have an object A that has as an instance variable a collection of
I have two classes: Action class, that has a method for executing VBScript files,
I have a JUnit4 test which extends AbstractTransactionalJUnit4SpringContextTests. I have a business object that
I have a vector class that has it's Equals(object obj) method overridden so that
have a Grails domain object that has a custom static function to grab data

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.