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

Sorting a 2-Dim array by first column.

Options
  • 07-04-2009 4:01pm
    #1
    Registered Users Posts: 320 ✭✭


    Hi,

    how I can sort a two dimensional array according to lowest to highest of the first column only?

    What I mean is I have a 2 dimensional array with the X and Y values. I then need to sort it by the X values only and the Y values should be in the same row as the X value before sorting.

    Like

    X Y
    3 18
    4 45
    1 32
    2 99

    after sorting by the X column the values should read as:

    X Y
    1 32
    2 99
    3 18
    4 45


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Well I guess there's a couple of approaches you could use here; but that would depend on the language and array representation.

    The only difference from a standard sort in theory here is that you're examining the first element of each array and then swapping the entire array as required... it's not that different to your bog standard sort unless you have performance restrictions.

    If you could post more details it might be easier for someone to give you some more concrete help.


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


    probably start with what language you want it in.

    Also is this class homework? If it is you will probably be expected to write the method yourself. If it isn't then you could just use a helper method for the related language.


  • Registered Users Posts: 320 ✭✭SoldierForce


    Sorry I thought I mentioned the language! I am using Java.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Sorry I thought I mentioned the language! I am using Java.

    Well conceptually all your doing is swapping arrays then; so you just examine the element of the array you're interested in and then you're swapping the array references based on your comparisons.

    What exactly are you having problems with? It's pointless someone coming along and giving you a solution if you're doing this as a learning exercise. It might also be nice to share some context and attempt code too.


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


    The easy way to do is is to implement Comparator interface in your javabean that contains X/Y. After that you add the method

    public int compare(Object o1, Object o2)

    That method should be the logic to compare the contents of one of the fields objects vs the other. you return 1 if o1 > o2, 0 if same and -1 if o1 < o2.

    then you just use Arrays.sort.

    ... you can also do ....

    implement Comparable then create the method
    public int compareTo(Object o2){

    Same deal again on the return and Arrays.sort. Think this is the better way to do it. (been a while)

    I've marked in bold what you should read up on.

    * Arrays.sort is the correct way to do it, but if this is homework you are generally required to write your own sort algorithm, so above would not be the way to do it but would give some hints.


  • Advertisement
Advertisement