Sorting Multi-Dim Array -- Help! 
Author Message
 Sorting Multi-Dim Array -- Help!

Help!  How do you sort a multi-dimensional array?  For example,
if you had a 2 dimensional array, e.g., array [i][j], what would
you do?  I've tried to find some examples and can't.  Is this
possible?

Diane Kresovich

--
Diane Kresovich



Fri, 07 Jan 2000 03:00:00 GMT  
 Sorting Multi-Dim Array -- Help!



Quote:
>Help!  How do you sort a multi-dimensional array?  For example,
>if you had a 2 dimensional array, e.g., array [i][j], what would
>you do?  I've tried to find some examples and can't.  Is this
>possible?

The first think you need to do is define what sorting a multidimensional
array means, i.e. what sort of ordering you want to put on the array
elements - there's no single answer to this. Try posting an example
of what you want to do e.g. a sample array and what it should look line
once sorted (and why).

--
-----------------------------------------


-----------------------------------------



Tue, 11 Jan 2000 03:00:00 GMT  
 Sorting Multi-Dim Array -- Help!



Quote:
> Help!  How do you sort a multi-dimensional array?  For example,
> if you had a 2 dimensional array, e.g., array [i][j], what would
> you do?  I've tried to find some examples and can't.  Is this
> possible?

> Diane Kresovich

> --
> Diane Kresovich

Diane...

The compare (... function for qsort (... would be the same for a
multi-dimensional array as for a single-dimensional array.

consider...

#include <stdio.h>
#include <stdlib.h>

/* 'compare (...'for 'qsort (...' */
int compare (const void *a, const void *b) {
  /* return (*(int *)a > *(int *) b) - (*(int *)a < *(int *) b); */
  if (*(int *) a > *(int *) b)
    return 1;
  if (*(int *) a < *(int *) b)
    return -1;
  return 0;

Quote:
}

int main (void) {
  int array[10][10];
  int i, j;
  for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++) {
      array[i][j] = rand () % 100;
      printf ("%2d ", array[i][j]);
    }
    printf ("\n");
  }
  qsort (array, sizeof array / sizeof **array, sizeof **array, compare);
  printf ("\n");
  for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++)
      printf ("%2d ", array[i][j]);
    printf ("\n");
  }
  return EXIT_SUCCESS;

Quote:
}

Regards
Brian


Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help on algorithm to translate a 2 dim array to a 1 dim aray index

2. casting 1 dim array to 2 dim array

3. multi-dim Array Redim question

4. Multi-dim arrays and pointers

5. Files-> multi dim arrays

6. pointers and multi-dim array

7. multi dim arrays?

8. multi-dim arrays

9. multi dim arrays

10. multi-dim array

11. Returning multi-dim arrays

12. Experts: Multi-dim dynamic arrays

 

 
Powered by phpBB® Forum Software