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.

3
  • uh how can the first example be correct, do you not want (one@2, two@0, three@1) to output (two, three, one) ? Commented Mar 4, 2011 at 1:01
  • Yes these are arraylists Commented Mar 4, 2011 at 1:54
  • The first example is correct. Its opposite of what you are thinking. I took the positions from my database and I want to map the mArrayName to match those positions (position in the array). Commented Mar 4, 2011 at 1:57

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh thank you, I have done this for my other apps but forgot about it for this one! This will help a bit as well.
1

You could use some form of tuple, some class to hold both id and name. You'll just to have a java.util.Comparator that compares it accordingly, both elements will move together and your code will be cleaner.

This data structure might be convenient for the rest of your program... if not, just take things off it again and you're done.

1 Comment

Sounds great! Now how do you do that.... hehe, could you provide a sample of code of how I would get the those arrays to move together to a desire configuration thats faster than what I have already? My brain only thinks in for loops and if statements.
0

If your order indexes are compact, i.e. from index 0 to size - 1, then just use an array and create the updated list afterwards? About something like

MyArray[] array = new MyArray[size];
for(int j=0;j< size;j++) {
   array[  mArrayPosition.get(j)   ] = mArrayName.get(j);
}

// create ArrayList from array

2 Comments

This is what I already have. Written your way it looks like this mArrayName.set(mArrayPosition.get(j), mArrayNameOriginal.get(j)); your way just gets rid of the temp variable.
If you are not going to follow Lungberg's approach (object orientation?), I suggest you use Maps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.