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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:22:17+00:00 2026-06-03T13:22:17+00:00

I have an [] that has some numbers (distances from some point). I want

  • 0

I have an [] that has some numbers (distances from some point).
I want to create an array of indexes into the first array where the indexes are sorted by the distance.

e.g.

suppose double[] dist=new double[5] {3.2, 1.4, 7.3, 2.2, 9.1};
then I want to get an array like this:

int[] sortedIndexes=new int[5] {1, 3, 0, 2, 4};

so if I want the second nearest distance I can check dist[sortedIndexes[1]].
I don’t want to sort the original array, just the array of indexes based on the distances.

UPDATE 1:
The Code I was trying looks like this:

Collections.sort(sortedIDXs, new Comparator<Integer>() {
    public int compare(int idx1, int idx2) {
        return Double.compare(distances[idx1], distances[idx2]);
    }
});

But I am getting several errors with it with the most “problematic” one being:
“Cannot refer to a non-final variable distances inside an inner class defined in a different method“

Thanks

  • 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-03T13:22:19+00:00Added an answer on June 3, 2026 at 1:22 pm

    You’re on the right track, but

    • You’re better off with an Integer array than an int array if you’re using a generic Comparator<Integer>.
    • You have to use Arrays.sort instead Collections.sort for sorting an array.
    • You have to make the distances variable final if it’s referenced in an anonymous inner class.

      final double[] distances=new double[]{3.2, 1.4, 7.3, 2.2, 9.1};
      Integer[] sortedIDXs  = new Integer[]{0,1,2,3,4};
      Arrays.sort(sortedIDXs, new Comparator<Integer>() {
          public int compare(Integer idx1, Integer idx2) {
              return Double.compare(distances[idx1], distances[idx2]);
          }
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list that has some chapter numbers in string. When I sort
I have a HUD window that has some labels on it, and I want
Hi I have an array list which has some numbers in it like {23,16,45,26,2,5,9}
Lets say I have some Django model that has a list of numbers as
I have to parse some numbers from file names that have no common logic.
I have a code that has some mpi api-dependent bits: #if MPIVERSION==1 ... #elif
We have a system that has some Bash scripts running besides Java code. Since
I have an app that has some configuration settings stored in SharedPreference. Now I
Imagine you have an entity that has some relations with other entities and you
So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE

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.