Results 1 to 5 of 5
 02072011, 01:40 AM #1Member
 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.
 02072011, 06:04 AM #2Member
 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.
 02072011, 08:31 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,401
 Blog Entries
 7
 Rep Power
 25
Build a wall around Donald Trump; I'll pay for it.
 02072011, 12:01 PM #4
i just tried out what you suggest and the algorith is ok but there is no 7th element in the y array.
 02072011, 12:48 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,401
 Blog Entries
 7
 Rep Power
 25
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]
Java Code:l: [2, 0, 4, 1, 3] // corresponding to: x: [1, 3, 4, 5, 7] y: [3, 4, 11, 6, 10]
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

Java ArrayList : Generics & Sorting
By Eranga in forum Java TutorialReplies: 0Last Post: 10192010, 04:43 AM 
Sorting ArrayList by object data
By drymsza1234 in forum New To JavaReplies: 2Last Post: 04152010, 01:22 AM 
Sorting printed ArrayList of user inputted strings.
By movsesinator in forum New To JavaReplies: 3Last Post: 04032010, 09:27 PM 
Sorting an ArrayList
By fleshboundbook in forum New To JavaReplies: 3Last Post: 02132010, 01:20 PM 
[SOLVED] is ArrayList zero based indexing?
By Nicholas Jordan in forum Advanced JavaReplies: 5Last Post: 07132008, 06:24 PM
Bookmarks