Autor Beitrag
tr3bor
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19



BeitragVerfasst: Sa 16.12.06 19:03 
Hallo,
ich möchte ein Mehrdimensionales Array Sortieren.
Ich habe ein 2 Dimensionales Array, in mehrere Datensätze sind(Zeilen) nun möchte ich die Datensätze so vertauschen das einw bestimmte Spalte Auf oder Absteigend sortiert ist. Nun meine frage wie mache ich das?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 16.12.06 20:56 
Moin!

Du tauscht einfach alle Elemente einer Zeile in deinem Dreieckstausch, fertig. Genaueres, wenn du etwas Code zeigst, wie du das bisher machst. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Sa 16.12.06 22:19 
Hallo,

aus einem TurboPascal Programm umgestrickt.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
unit dim2Sort;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  tZeileIdx = 1..7;
  tSpalteIdx = 1..7;
  tData  = double;
  tSpalte = array [tSpalteIdx] of tData;

  tmy2dimarray = array [tZeileIdx] of tSpalte;

var
  Form1: TForm1;

implementation

{$R *.dfm}
var

  my2dimFeld : tmy2dimarray;


PROCEDURE SortiereNachSpalte(var Feld   :tmy2dimarray;
                                 Spalte : tSpalteIdx);
var
  VgData     : tData;
  TauschData : tSpalte;

  procedure QSort(Links, Rechts : tZeileIdx);
  var
     i, j: tZeileIdx;
  begin
      i := Links;
      j := Rechts;
      VgData := Feld[(Links+Rechts) shr 1][Spalte];
         repeat
            while VgData>Feld[i][Spalte] do
               Inc(i);
            while VgData<Feld[j,Spalte] do
              Dec(j);
            if i <= j then
              begin
              if i <> j then
                begin
                //Komplette Zeilen tauschen
                TauschData := Feld[i];
                Feld[i] := Feld[j];
                Feld[j]:= TauschData;
                 end;
              Inc(i);
              Dec(j);
            end{if}
         until i > j;
         {Immer die kleinere Haelfte zuerst sortieren,
         geringere Rekursionstiefe}

         if (j-Links)< (Rechts-i) then
         begin
             if Links < j then
                QSort(Links,j);
             if Rechts > i then
                QSort(i,Rechts);
         end  {if}
         else
         begin
            if Rechts > i then
               QSort(i,Rechts);
            if Links < j then
               QSort(Links,j);
         end;{else}
      end;
BEGIN
 QSOrt(low(tZeileIdx),High(tZeileIdx));
END;

procedure AusgabeFeld(var Feld:tmy2dimarray);
var
  i : tZeileIdx;
  j : tSpalteIdx;
  s : string;
begin
  s := '';
  Form1.Memo1.Lines.Add(s);
  For i := low(i) to High(i) do
    begin
    s := s+Format('%7d ',[i]);
    For j := low(j) to High(j) do
      s := s+Format('%10.7f ',[Feld[i,j]]);
    Form1.Memo1.Lines.Add(s);
    s :='';
    end;
  Form1.Memo1.Lines.Add(s);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i : tZeileIdx;
  j : tSpalteIdx;

begin
  memo1.Clear;
  randomize;
  for i := Low(tZeileIdx) to High(tZeileIdx) do
   for j := Low(tSpalte) to High(tSpalte) do
      my2DimFeld[i,j] := random;
  Memo1.Lines.Add('Unsortiert');

  AusgabeFeld(my2DimFeld);

  for j := Low(tSpalte) to High(tSpalte) do
    begin
    Memo1.Lines.Add('Sortiert nach Spalte '+Inttostr(j));
    SortiereNachSpalte(my2DimFeld,j);
    AusgabeFeld(my2DimFeld);
    end;
end;

end.


Gruss Horst
tr3bor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19



BeitragVerfasst: Mo 18.12.06 18:43 
Titel: Lösung
HI,
Danke für eure Antworten ich steige da aber nicht durch :(
ich habe das Problem jetzt einfach so Gelöst, dass ich jeweils ein Array pro Spalte habe und die kann man dann ja ganz leicht sortieren.
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 18.12.06 20:10 
du sortierst erst alle reihen und dann einmal die erste spalte, fertig.

mfg
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mo 18.12.06 20:29 
Hallo,

ich habe es so verstanden das die Zeile erhalten bleiben soll.
sei array:
012
120
201
dann ergibt Sortierung nach Spalte 3
120 (ex Zeile 2)
201 (ex Zeile 3)
012 (ex Zeile 1)

Du verlierst diese Ordnung.

Gruss Horst
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 18.12.06 20:33 
Titel: Re: Lösung
user profile icontr3bor hat folgendes geschrieben:
HI,
Danke für eure Antworten ich steige da aber nicht durch :(
ich habe das Problem jetzt einfach so Gelöst, dass ich jeweils ein Array pro Spalte habe und die kann man dann ja ganz leicht sortieren.

Naja, Hauptsache es funktioniert... Aber wenn du gezeigt hättest, wie dein Quelltext aussieht und wie du die Sortierung versucht hast, dann hätten wir dir auch helfen können, es so zu machen, wie du es eigentlich wolltest.

Und naja, wenn du wo was nicht verstehst, kannst du ruhig fragen. ;-)
Wenn du zeigst, was du versucht hast und konkret sagst, wo dein Problem ist, also wo du nicht weiterkommst, dann findest du hier im Forum genug Helfer, die dir das erklären (merkst du ja auch ;-)). Und dann kommst du das Schritt für Schritt zum Ziel. Nur: wenn du keinen Quelltext zeigst und zu gezeigtem Quelltext nur sagst, dass du da nicht durchsteigst, nun ja, wie sollen wir dir da helfen...

Das wollte ich dann doch mal dazu sagen, auch im Hinblick auf weitere Fragen von dir...
tr3bor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19



BeitragVerfasst: Mo 18.12.06 23:11 
ja also ich an relevantem Quelltext hatte wohl nur das Mehrdimensionale Array. und ich wollte die zeilen halt gleich behalten. Ich habe das nun so gelöst:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
laenge := length(jobs_time);
for ie := laenge DownTo 1 Do
      Begin
        for oe := 0 To laenge-1 Do
        Begin
          if StrToInt(jobs_time[oe]) > StrToInt(jobs_time[oe+1]) then
          Begin
            tausche(jobs_time[oe],jobs_time[oe+1]);
            tausche(jobs_con[oe],jobs_con[oe+1]);
            tausche(jobs_emp[oe],jobs_emp[oe+1]);
            tausche(jobs_id[oe],jobs_id[oe+1]);
          end;
        end;
      end;

Habe den alt bekannten BubbleSort benutzt.