AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Sortieren von STRINGGRID klappt nicht.
Thema durchsuchen
Ansicht
Themen-Optionen

Sortieren von STRINGGRID klappt nicht.

Ein Thema von wschrabi · begonnen am 20. Okt 2016 · letzter Beitrag vom 20. Okt 2016
Antwort Antwort
wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#1

Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:33
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.
Angehängte Dateien
Dateityp: zip bug.zip (53,1 KB, 0x aufgerufen)
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#2

AW: Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:51
Was heißt "klappt nicht"?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.537 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:53
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?
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#4

AW: Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:58
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?
  Mit Zitat antworten Zitat
wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#5

AW: Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 12:59
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.
  Mit Zitat antworten Zitat
wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#6

AW: Sortieren von STRINGGRID klappt nicht.

  Alt 20. Okt 2016, 13:01
ACH DANKE!!! Dachte man muss sort noch aufrufen. Doppeltgemobbelt.
  Mit Zitat antworten Zitat
Antwort Antwort

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:33 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