Autor Beitrag
FrankBurian
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 57



BeitragVerfasst: Sa 11.02.06 15:36 
Hallo,

ich würde gern mein Array sortieren :
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
type
  arrRec = record
    flag        : integer;
    title,
    description : string;
  end;

var
  arr_data    : array[1..10] of arrrec;


allerdings will ich nach 2 Werte sortieren:

Primär "flag" und secondär "title"!

Bestes Beispiel wie die SQL-Anweisung :
ausblenden SQL-Anweisung
1:
... ORDER BY flag,title ASC					


Kennt jemand eine fertige gute Funktion bzw. Algo dafür???

MfG
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 11.02.06 15:50 
Ja. Nennt sich Quicksort. Du findest dazu mit Sicherheit Code zu im Forum. Einziges Problem: Da werden immer nur Zahlen sortiert. Du musst dann (ganz grob gesagt) alle Stellen, wie if Zahl1 > Zahl2 durch eine eigene Funktion if Compare(Rec1, Rec2) > 0 ersetzen, die in etwa so aussieht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
function Compare(Rec1, Rec2: TMyRecord): Integer;
// Rückgabe Werte:
// -1:  1 kleiner 2
//  0:  1 = 2
//  1:  1 größer 2
begin
  result := 0;
// Property 1 ist die "Hauptsortiereigenschaft"
  if Rec1.Property1 > Rec2.Property1 then 
    result := 1  // <=> ist größer
    else 
    if Rec1.Property1 < Rec2.Property1 then
      result := -1 // <=> isat kleiner
    else // Haupteigenschaft gleich
    begin
      if Rec1.Property2 > Rec2.Property2 then 
        result := 1  // <=> ist größer
        else 
        if Rec1.Property2 < Rec2.Property2 then
          result := -1 // <=> ist kleiner
    end 
end;

_________________
We are, we were and will not be.