Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Sortieren von STRINGGRID klappt nicht. (https://www.delphipraxis.net/190618-sortieren-von-stringgrid-klappt-nicht.html)

wschrabi 20. Okt 2016 12:33

Sortieren von STRINGGRID klappt nicht.
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo Freunde,
habe Seattle und da will ich in meinem Prog bei klicken auf die Stringgridheaderzeile die Spalte richtig (dh bei Strings alfanumerisch bei ZAhlen nur numerisch) sortieren. Doch es klappt nicht. Weiß wer Rat? Besten DANK

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid9: TStringGrid;
    procedure FormCreate(Sender: TObject);
   procedure StringGrid9MouseUp(Sender: TObject; Button:
      TMouseButton;  Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }

   procedure SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Array of Integer);
   
  end;

var
  Form1: TForm1;
  function StrCmpLogical(const s1, s2: string): Integer;

implementation



{$R *.dfm}

function StrCmpLogicalW(psz1, psz2: PWideChar): Integer; stdcall; external 'shlwapi.dll';

function StrCmpLogical(const s1, s2: string): Integer;
begin
  //result:= StrCmpLogicalW(PWideChar(WideString(s1)), PWideChar(WideString(s2)));
  Result := StrCmpLogicalW(PChar(s1), PChar(s2));
end;  

function CustomCompareLogical(List: TStringList;
  Index1, Index2: Integer): Integer;
var
   tmpstring1, tmpstring2: string;
   value1, value2: double;
begin

   tmpstring1:= Copy(List[index1],1,Pos('@',List[index1])-1);
   tmpstring2:= Copy(List[index2],1,Pos('@',List[index2])-1);
   if ((TryStrTofloat(tmpstring1, value1)) and (TryStrTofloat(tmpstring2, value2))) then
      begin
      if value1 > value2 then
            Result:=1
      else if value1 < value2 then
            result:=-1
      else if value1 = value2 then
            result:=0;
      end
  else    
   Result := StrCmpLogical(List[Index1], List[Index2]);
 
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid9.Cells[0,0]:='Zeile';
  StringGrid9.Cells[1,0]:='Scopus Title';
  StringGrid9.Cells[2,0]:='similar Catalog Title';
  StringGrid9.Cells[3,0]:='Levensthein Distance';
  StringGrid9.Cells[4,0]:='rel. LS Distance [%]';
   StringGrid9.ColWidths[1]:=250;
   StringGrid9.ColWidths[2]:=250;
   StringGrid9.ColWidths[3]:=140;  
   StringGrid9.ColWidths[4]:=140;  

  StringGrid9.Cells[0,1]:='1';
  StringGrid9.Cells[1,1]:='AAAAAA Title';
  StringGrid9.Cells[2,1]:='similar Catalog Title';
  StringGrid9.Cells[3,1]:='1';
  StringGrid9.Cells[4,1]:='0,1';
  StringGrid9.Cells[0,2]:='2';
  StringGrid9.Cells[1,2]:='BBBBBB Title';
  StringGrid9.Cells[2,2]:='similar Catalog Title';
  StringGrid9.Cells[3,2]:='3';
  StringGrid9.Cells[4,2]:='0,3';
  StringGrid9.Cells[0,3]:='22';
  StringGrid9.Cells[1,3]:='CCCCCC Title';
  StringGrid9.Cells[2,3]:='similar Catalog Title';
  StringGrid9.Cells[3,3]:='22';
  StringGrid9.Cells[4,3]:='0,22';
  StringGrid9.Cells[0,4]:='3';
  StringGrid9.Cells[1,4]:='DDDDDD Title';
  StringGrid9.Cells[2,4]:='similar Catalog Title';
  StringGrid9.Cells[3,4]:='4';
  StringGrid9.Cells[4,4]:='0,4';
   
end;

procedure TForm1.StringGrid9MouseUp(Sender: TObject; Button:
TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  ACol, ARow: Integer;
begin
  StringGrid9.MouseToCell(x, y, ACol, ARow);
  if ((Arow=0) ) then
      begin
      SortStringGrid(Stringgrid9,ACol);
      end;
   
  (* 
  if ARow <= StringGrid9.FixedRows-1 then
    ShowMessage('Clicked on Fixed Row');
    *)
end;

procedure TForm1.SortStringGrid(GenStrGrid: TStringGrid; ThatCol: Array of Integer);
const
  // Define the Separator
  TheSeparator = '@';
var
  CountItem, I, J, K, ThePosition: integer;
  MyList: TStringList;
  MyString, TempString: string;
begin
  // Give the number of rows in the StringGrid
  CountItem := GenStrGrid.RowCount;
  //Create the List
  MyList := TStringList.Create;
  MyList.Sorted := False;
  try
    begin
    (*
      MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort; *)
   
      for I := 1 to (CountItem - 1) do
        MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol[0]] + TheSeparator +
          GenStrGrid.Rows[I].Text);
      MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort;

      for K := 1 to Mylist.Count do
      begin
        //Take the String of the line (K – 1)
        MyString := MyList.Strings[(K - 1)];
        //Find the position of the Separator in the String
        ThePosition := Pos(TheSeparator, MyString);
        TempString := '';
        {Eliminate the Text of the column on which we have sorted the StringGrid}
        TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
        MyList.Strings[(K - 1)] := '';
        MyList.Strings[(K - 1)] := TempString;
      end;

      // Refill the StringGrid
      for J := 1 to (CountItem - 1) do
        GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
       
       
    end;
  finally
    //Free the List
    MyList.Free;
  end;
end;



end.

Luckie 20. Okt 2016 12:51

AW: Sortieren von STRINGGRID klappt nicht.
 
Was heißt "klappt nicht"? :roll:

DeddyH 20. Okt 2016 12:53

AW: Sortieren von STRINGGRID klappt nicht.
 
Das hab ich mich auch gefragt, außerdem:
Zitat:

Delphi-Quellcode:
 MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort;

Wieso erst so sortieren und direkt danach nochmal anders?

wschrabi 20. Okt 2016 12:58

AW: Sortieren von STRINGGRID klappt nicht.
 
Naja click mal auf LINE (Spalte 0)
Dann ist 1,2,22,3 und richti gis 1,2,3,22
er sortiert nicht numerisch.
Weiß wer Rat?

wschrabi 20. Okt 2016 12:59

AW: Sortieren von STRINGGRID klappt nicht.
 
Zitat:

Zitat von DeddyH (Beitrag 1351488)
Das hab ich mich auch gefragt, außerdem:
Zitat:

Delphi-Quellcode:
 MyList.customsort(CustomCompareLogical);
      //Sort the List
      Mylist.Sort;

Wieso erst so sortieren und direkt danach nochmal anders?

Naja die 2 Möglichkeit hat alles Spalten in Sinn. Jenachdem welche SpaltenHeader man anklickt.

wschrabi 20. Okt 2016 13:01

AW: Sortieren von STRINGGRID klappt nicht.
 
ACH DANKE!!! Dachte man muss sort noch aufrufen. Doppeltgemobbelt.
:-D


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:20 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz