Results 1 to 5 of 5
- 02-07-2011, 12:40 AM #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.
- 02-07-2011, 05:04 AM #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.
- 02-07-2011, 07:31 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-07-2011, 11:01 AM #4
i just tried out what you suggest and the algorith is ok but there is no 7th element in the y array.
- 02-07-2011, 11:48 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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:
and after sorting, l contains the following elements:Java Code:l: [0, 1, 2, 3, 4] x: [3, 5, 1, 7, 4] y: [4, 6, 3, 10, 11]
kind regards,Java Code:l: [2, 0, 4, 1, 3] // corresponding to: x: [1, 3, 4, 5, 7] y: [3, 4, 11, 6, 10]
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java ArrayList : Generics & Sorting
By Eranga in forum Java TutorialReplies: 0Last Post: 10-19-2010, 04:43 AM -
Sorting ArrayList by object data
By drymsza1234 in forum New To JavaReplies: 2Last Post: 04-15-2010, 01:22 AM -
Sorting printed ArrayList of user inputted strings.
By movsesinator in forum New To JavaReplies: 3Last Post: 04-03-2010, 09:27 PM -
Sorting an ArrayList
By flesh-bound-book in forum New To JavaReplies: 3Last Post: 02-13-2010, 12:20 PM -
[SOLVED] is ArrayList zero based indexing?
By Nicholas Jordan in forum Advanced JavaReplies: 5Last Post: 07-13-2008, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks