Thread: sorting a 2d array by column

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    sorting a 2d array by column

    Hey i have a matrix containing a lot of points and each point has its coordinates x and y. That is a nx2 size array. I want to sort it according to the first column ascending, with x coordinates. For points that have the same x coord i would like to sort according to y coord. Here is what i did and i cannot get a good result.
    Code:
    #include <stdio.h>                                 
    #include <stdlib.h>                                
    int main(){
    int a[5][2] = {{1,0}, {4,2}, {2,4}, {8,6},{4,8}};
    int temp=0;
    int i=0;
    int j=0;
    
    for(int y=0; y<2; y++)
        {
    	for(int x=0; x<5;x++)	
    		{
    	
            if(a[x][y] < a[0][y])
                {
                    temp=a[x][y];
                    a[0][0]=a[x][y];
                    a[0][0]=temp;
                
                }
            }
        }
    
       for ( i = 0; i < 5; i++ )
       {
          for ( j = 0; j < 2; j++ )
          {
             printf("a[%d][%d] = %d\n", i,j, a[i][j] );
          }
       }
    return 0;}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you looked at sorting before at all? Do you have an algorithm in mind for how you want to sort?

  3. #3
    Registered User Andam's Avatar
    Join Date
    Oct 2013
    Posts
    1
    You can put it all in 1D array then sort and put it in the matrix again as you want it.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    ok done it here it is
    Code:
    	for(int xcord=0;xcord<5;xcord++){
    		for(int ycord=xcord+1;ycord<5;ycord++){
    			if(a[xcord][0]>a[ycord][0]){
    			
    				int temp = a[xcord][1];
    				int temp2=a[xcord][0];
    				a[xcord][0]=a[ycord][0];
    				a[xcord][1]=a[ycord][1];
    				a[ycord][0]=temp2;
    				a[ycord][1]=temp;
    				}
    		}
    	}

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Andam View Post
    You can put it all in 1D array then sort and put it in the matrix again as you want it.
    That's just crazy talk.


    In C, it would normally be done like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ARRSIZE(arr) (sizeof(arr)/sizeof(*(arr)))
    
    int compare(const void *a, const void *b) {
        int x1 = *(const int*)a;
        int x2 = *(const int*)b;
        if (x1 > x2) return  1;
        if (x1 < x2) return -1;
        // x1 and x2 are equal; compare y's
        int y1 = *(((const int*)a)+1);
        int y2 = *(((const int*)b)+1);
        if (y1 > y2) return  1;
        if (y1 < y2) return -1;
        return 0;
    }
    
    int main(void) {
        int matrix[][2] = {{8,6}, {4,2}, {1,0}, {4,8}, {2,4},
                           {4,3}, {1,2}, {2,2}, {8,3}, {5,5}};
    
        printf("Original: ");
        for (size_t i = 0; i < ARRSIZE(matrix); i++)
            printf("(%d,%d) ", matrix[i][0], matrix[i][1]);
        putchar('\n');
    
        qsort(matrix, ARRSIZE(matrix), sizeof(*matrix), compare);
    
        printf("Sorted  : ");
        for (size_t i = 0; i < ARRSIZE(matrix); i++)
            printf("(%d,%d) ", matrix[i][0], matrix[i][1]);
        putchar('\n');
    
        return 0;
    }
    Last edited by oogabooga; 10-24-2013 at 09:16 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtracting one column in a 2d array from another?
    By zaihed13 in forum C Programming
    Replies: 6
    Last Post: 10-19-2013, 09:29 AM
  2. Adding a new column in array
    By Tomislav Pti in forum C Programming
    Replies: 3
    Last Post: 11-22-2012, 04:48 AM
  3. Reading a text file and sorting a column of numbers
    By txmusic in forum C Programming
    Replies: 9
    Last Post: 02-26-2012, 09:49 PM
  4. adding up array column total
    By dmckay in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:14 PM
  5. Replies: 10
    Last Post: 07-13-2003, 01:48 PM