Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Sorting A 2D Array [Java]

  • 16-03-2003 2:24pm
    #1
    Registered Users, Registered Users 2 Posts: 509 ✭✭✭


    Hey there,

    Right. I've got a little part of an assignment left which is the only part I've now got a problem with. I've got a 2D array. One column with strings, the other with integers. What I want to do is sort the integers column, but I want that sorting to be applied to the other column of the array to.

    This might make what I'm asking clearer:

    Z 6
    Q 2
    Y 3

    Want to arrange so that:

    Z 6
    Y 3
    Q 2

    In other words, I want the letters to get sorted so that they're tied to the same corresponding numbers.

    I'm using the java.util.Arrays.sort(Arr); method to arrange the array. Another problem. It arranges them decsending, wheras I need to get it to go ascending. But that's a problem I'm sure I can deal with myself, although if you guys have any input, it'd help a lot.

    Thanks in advance for any replies, and feel free to ask me for more info if need's be.

    Cheers!


Comments

  • Registered Users, Registered Users 2 Posts: 2,077 ✭✭✭parasite


    what's the relationship between the numbers and the strings, you'll have to write your own sorting loop i'd imagine, not the default
    :confused:


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Use System.arraycopy()


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Check out some sorting algorithms on the web.

    Bubble sorting will probably do the job well for you. The main idea is that you will have a for loop:
    for(int i = 0; i < arr1.length; i++) {
    ..........
    if(.........) {
        swop(arr1, i, i-1);
    }
    .......
    }
    
    where the local swop() method swops the elements at indexes i, i-1 of the supplied array. So instead of just swopping the elements in one array, you swop the elements in both. Basically, any movement of elements you make to the number array, you mirror the change in the letters array.

    Easy :)


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    I'm going to presume that we are talking about a single two dimensional array of objects here, with Integers and Strings. You should still be able to use the java.util.Arrays class to sort the two dimensional array, but you will have to supply your own comparator.

    So we have something like:
    Object[][] arr = new Object[100][2];
    
    with arr[x][0] being a String and arr[x][1] being an Integer. You could write your own Comparator that looks something this
    class MyComparator implements java.util.Comparator
    {
    	public int compare( Object o1, Object o2 )
    	{
    		Integer i1 = (Integer)Array.get( o1, 1 );
    		Integer i2 = (Integer)Array.get( o2, 1 );
    		return i1.compareTo( i2 );		
    	}	
    }
    
    where Array is java.lang.reflect.Array. This takes the two dimenstional array as an array of arrays. So each element in the arr array is an array of two objects, the second being an Integer. We just have to sort these arrays by their second - Integer - element.

    Then all you have to do is sort the array with something along the lines of
    java.util.Arrays.sort( arr, new MyComparator() );
    

    Zab.

    ps: the java.util.Array.sort methods sort into ascending order, not descending.


Advertisement