Hello,

I'm really a beginner in programming, and i might need you're help.
I have an array[position][3] with fixed coloumns
Position is from 0 to 99 an it looks like this:

array [position][0] = helpFlow;
array [position][1] = i; //i=rows of a [10[10]-grid
array [position][2] = j; //j=column of a [10][10]-grid


So it looks like
0 {5 0 0}
1 {0 0 1}
2 {2 0 2}
3 {7 0 3}
........

The first column, helpFlow, i want to sort from small to large.
But I have no idea how to do that.

Perhaps ist helps if I give you a part of my code

#include <list.h>
#include <vcl.h>
#pragma hdrstop
#include <iostream>  //to use cin / cout
#include <string>    //to use strings
#include <math.h>    //to use mathematical functions
#include <fstream>   //to read and write files
#include <cstdlib>   //to use exit()
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <map>
#include "Unit.h"
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   calculate_RunonCells();
}

//--------------------------------------------------------------------------------

void TForm1::calculate_RunonCells()
{
        for (int i=0; i< xsize * ysize;i++)
            {//1
                array[i] [0] =-1;
                array[i] [1] =-1;
                array [i] [2] =-1;
             }//1

        for (int i=0; i<xsize; i++)
          for (int j=0; j<ysize; j++)
            {
                for (int k=max(0,i-1); k<=min(i+1,xsize-1); k++)
                for (int l=max(0,j-1); l<=min(j+1,ysize-1); l++)
                {
                  if (((direction[k][l]-direction[k][l]%ysize)/ysize == i) && (direction[k][l]%ysize == j))

                  {
                     surroundFlow[i][j]++;
                  }

                }

                     helpFlow = surroundFlow[i][j];

                    array [position][0] = helpFlow;
                    array[position][1] = i;
                    array [position][2] = j;
                     position++;
            }

}
William Hemsworth commented: Perfect first post :] +7
Member Avatar for r.stiltskin

Conceptually it's the same as sorting a 1D array, since the 2D array is an array of 1D arrays.

Say you want to use bubble sort. So write a "swap3" function that takes two 3-element int arrays and swaps them element by element, i.e.:
arr1[0] <--> arr2[0]
arr1[1] <--> arr2[1]
arr1[2] <--> arr2[2]

Then write your bubble sort function, comparing the first element of each 3-element array with its neighbor, swapping them if the first element of the first array is greater than the first element of the neighbor. It's just like an ordinary bubble sort, except that when you swap you're swapping two 3-element arrays instead of two individual elements, and the sorting comparison will look like:
if( arr[0] > arr[i+1][0] ) { ...
instead of
if( arr > arr[i+1] ) { ...

Great! Thanks a lot!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.