Results 1 to 5 of 5
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default 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.

  2. #2
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by busdude View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by j2me64 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java ArrayList : Generics & Sorting
    By Eranga in forum Java Tutorial
    Replies: 0
    Last Post: 10-19-2010, 04:43 AM
  2. Sorting ArrayList by object data
    By drymsza1234 in forum New To Java
    Replies: 2
    Last Post: 04-15-2010, 01:22 AM
  3. Sorting printed ArrayList of user inputted strings.
    By movsesinator in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 09:27 PM
  4. Sorting an ArrayList
    By flesh-bound-book in forum New To Java
    Replies: 3
    Last Post: 02-13-2010, 12:20 PM
  5. [SOLVED] is ArrayList zero based indexing?
    By Nicholas Jordan in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2008, 06:24 PM

Posting Permissions

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