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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:12:18+00:00 2026-05-20T11:12:18+00:00

I need to sort an array based on the positions held in another array.

  • 0

I need to sort an array based on the positions held in another array.

What I have works, but it is kinda slow, is there a faster/better way to implement this?

2 Parts:

Part1

int i = mArrayName.size(); 
int temp = 0;
for(int j=0;j<i;j++){
            temp = mArrayPosition.get(j);
            mArrayName.set(temp, mArrayNameOriginal.get(j));
        }

In this part, mArrayPosition is the position I would like the mArrayName to be in.

Ex.
input:
mArrayName= (one, two, three)
mArrayPosition = (2,0,1)

output:
mArrayName= (three, one two)

Part 2

int k=0;
int j=0;
do{
    if(mArrayName.get(k)!=mArrayNameOriginal.get(j)){
        j++;
    }else{

        mArrayIdNewOrder.set(k, mArrayId.get(j)); 
            k++;
            j=0;
        }
    }while(k < mArrayName.size());
}

In this part, mArrayName is the reordered name array, mArrayNameOriginal is the original name array.

Ex.
mArrayName = (three, one, two)
mArrayNameOriginal = (one, two, three)

Now I want to compare these two arrays, find which entries are equal and relate that to a new array that has their rowId number in it.

Ex.
input:
mArrayId = (001,002,003)

output:
mArrayIdNewOrder = (003,001,002)

So then I will have mArrayIdNewOrder id’s matching up with the correct names in mArrayName.

Like I said these methods work, but is there a faster/better way to do it? I tried looking at Arrays.sort and comparators but they only seem to sort alphabetically or numerically. I saw something like I can create my own rules inside the comparator but it would probably end up being similar to what I already have.

Sorry for the confusing question. I’ll try to clear up any ambiguities if needed.

  • 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-20T11:12:19+00:00Added an answer on May 20, 2026 at 11:12 am

    The best performance read I’ve found is Android’s Designing For Performance doc. You are violating a couple of the “Android way” style of doing things that will help you.

    You are using multiple internal getters inside each loop for what looks like a simple value. Redo this by accessing the fields directly.

    For extra credit, post your performance comparison results! I’d love to see em!

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

Sidebar

Related Questions

I have an array of filenames and need to sort that array by the
I need to sort arrays inside an array of array based on one of
I need to sort an array of ints using a custom comparator, but Java's
Hay, i have an array of objects and i need to sort them (either
I need to sort tuples according to the second element of each tuple but
I need some sort of interactive chart control for my .NET-based web app. I
I need some sort of node-graph editor, that hopefully works on both Mac and
I have a gridview and I need to sort its elements when the user
I have an IList<T> that I need to sort, and I would rather not
I need to sort an array containing arrays in ascending numerical order. The 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.