# Thread: sorting arraylist based on another arraylist

1. Member Join Date
Oct 2008
Posts
25
Rep Power
0

## sorting arraylist based on another arraylist

how can i sort 2 arraylist but only using 1 of them as criteria.

Like if I have arraylists and want to sort it based on the integers of x in ascending order.
x: [3,5,1,7,4]
y: [4,6,3,10,11]

I need to sort it so the arraylists become
x: [1,3,4,5,7]
y: [3,4,11,6,10]

so arraylist y is sorted according to x's ascending status, (meaning that all changes of x are transferred to y, so if for example x's 3rd element is moved to first position, y's 3rd element is moved to 1st as well, regardless of y's element's size.  Reply With Quote

2. Member Join Date
Feb 2011
Posts
15
Rep Power
0

## If it is a short list like that, I would use a bubble sort based on the first value.

Here is a piece of code that I use to do exactly that:

while(swapped)
{
swapped = false;
x++;
for (int i = 0; i < handSize - 1; i++)
{
if(arrayValues[i] > arrayValues[i + 1])
{
temp = arrayValues[i];
tempTwo = arraySuits[i];

arrayValues[i] = arrayValues[i+1];
arrayValues[i+1] = temp;

arraySuits[i] = arraySuits[i+1];
arraySuits[i+1] = tempTwo;

swapped = true;
}
}
}

What I am sorting is a very small list, if you have a large list then I would not suggest bubble sort, it is inefficient. I know this is an array and not an ArrayList but I can't see why this wouldn't work for your situation.

Good luck.  Reply With Quote

3. ##  Originally Posted by busdude how can i sort 2 arraylist but only using 1 of them as criteria.

Like if I have arraylists and want to sort it based on the integers of x in ascending order.
x: [3,5,1,7,4]
y: [4,6,3,10,11]

I need to sort it so the arraylists become
x: [1,3,4,5,7]
y: [3,4,11,6,10]

so arraylist y is sorted according to x's ascending status, (meaning that all changes of x are transferred to y, so if for example x's 3rd element is moved to first position, y's 3rd element is moved to 1st as well, regardless of y's element's size.
Create a small class that stores just an index 'i' and is Comparable on x[ i ]. Sort a list of them using one of the available sorting algorithms and at the end the index values point to the sorted elements (both in x and y).

kind regards,

Jos  Reply With Quote

4. ## i just tried out what you suggest and the algorith is ok but there is no 7th element in the y array.  Reply With Quote

5. ##  Originally Posted by j2me64 i just tried out what you suggest and the algorith is ok but there is no 7th element in the y array.
If you're replying to me: you misunderstood my suggestion: create a small class that just stores an index value; it corresponds to one element in the x array and its Comparable implenentation compares the element x[ i ] where i is the stored index. Sort a List of those little objects. Afterwards that List contains the index values of the elements of array x as if it where sorted.

if we call the list l, then before sorting:

Java Code:
```l: [0, 1, 2, 3, 4]
x: [3, 5, 1, 7, 4]
y: [4, 6, 3, 10, 11]```
and after sorting, l contains the following elements:

Java Code:
```l: [2, 0, 4, 1, 3]
// corresponding to:
x: [1, 3, 4, 5, 7]
y: [3, 4, 11, 6, 10]```
kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•