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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:00:41+00:00 2026-06-08T07:00:41+00:00

I’m touching up a game engine I wrote in Java a while back. I

  • 0

I’m touching up a game engine I wrote in Java a while back. I have a master Arraylist that holds Sprite objects. The Sprite class holds an X and Y location of the specified sprite by the type float.

I’m trying to sort the list based on their relative distance to a specific sprite (the player). I have a method that calculates the distance between 2 sprites.

/**Retrieve a double value that represents the distance between 2 sprites*/
public static double getSpriteDistance(Sprite s1, Sprite s2) {
    return Math.hypot((s1.getX() - s2.getX()), (s1.getY() - s2.getY()));
}

I have another method that I pass the Sprite used to sort based on its position as well as the master list of all the sprites (will be about 500-700 Sprites on the list depending on the map)

/**Sort a Sprite list relatively based on a specific sprite's location*/
public static ArrayList<Sprite> relativeSort(Sprite s1, ArrayList<Sprite> list) {             
    //Sort
    return list;
}

This is where I generally get stuck as far as how to do it, and looking into the future as far as efficiency and speed. I have a method to measure the distance between 2 sprites, now I just need the to sort the list so the specific sprite to be sorted is the first item on the list, and the sprite that’s the farthest away is the last.

I’ve thought of a using a recursive method to sort, or a while loop, but I feel as if there are better ways. I read about using Comparators and Collection’s sort() method. But the examples i’ve seen are very basic.

Thanks for any help, if you have any questions you need answered to better assist me, i’ll be glad to answer them.

EDIT:
I have a feeling someone will link this to me, so I’ll just clarify that I know about it, but have trouble understanding how to implement what I specified above using this:

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

  • 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-08T07:00:42+00:00Added an answer on June 8, 2026 at 7:00 am

    You’ll need to create a Comparator that follows this signature:

    public interface Comparator<Sprite> {
        public int compare(Sprite obj1, Sprite obj2);
    }
    

    The method compare should return a positive number if obj1 is to be sorted first in the list, and a negative number otherwise.

    Then you can pass it into Collections.sort().


    You can create a class on-the-fly like that pretty easily, actually:

    /**Sort a Sprite list relatively based on a specific sprite's location*/
    public static ArrayList<Sprite> relativeSort(Sprite s0, ArrayList<Sprite> list) {             
    
        Comparator<Sprite> comp = new Comparator<Sprite>() {
            public int compare(Sprite s1, Sprite s2) {
                //return a positive number if s1 is closer to s0
                //return a negative number if s2 is closer to s0
            }
        };
        Collections.sort(list, comp);
        return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.