Autor Beitrag
Timbo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 166



BeitragVerfasst: Sa 05.08.06 20:59 
Hallo,

also sowas soll raus kommen:

1
2
10
100
Hallo
kann
Toll

oder

0,1 €
1,22 €
5,12 €
10 €

Die CompareText Funktion kann das ja nicht.
Sql Sortiert das ja ohne Probleme.

Vielen Dank.
Bernhard Geyer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 721
Erhaltene Danke: 3



BeitragVerfasst: Sa 05.08.06 22:14 
SQL kann das da es ja auch weis welchen Typ von Feld es vor sich hat (jedenfalls wenn es Währungsbeträge oder Zahlentypen sind. Das bei SQL auch für ein Nvarchar-Feld deine erste sortierung richtig durchgeführt werden sollte wäre mir neu.
Timbo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 166



BeitragVerfasst: Sa 05.08.06 23:12 
Ok, kann sein, aber der Datei-Explorer z.B. Sortiert Dateinamen genau so wie beschrieben und das sind ja nun mal alles Strings.
Da muss es doch was geben, bin doch wohl nicht der erste, der eine vernüpftige Compare Funktion sucht???


Wollte da jetzt nicht was selbst schricken, aber man könnte doch den String von vorn durchgehen, und so lange es möglich ist daraus eine Zahl zu bilden, den vergleich auch als Zahl machen. Wenn dann Zahl mit nur String verglichen wird hat Zahl vorrang. Außerdem nur als UpperCase vergleichen...

Wird nur ganz schön auf die Performance gehen.
Axxus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26

Win XP
Delphi 2005 PE
BeitragVerfasst: Sa 05.08.06 23:44 
Hi

Willst du die Zahlen bzw Worte nur Ortnen oder es mithilfe einer Comparefunktion machen
Beim Ersten kann ich dir helfen aber beim zweiten nicht da ich nicht weiß was eine Comparefunktion ist vll kann mir das jmn hier sagen

Axxus

_________________
Adjektive und Adverbien sind relativ im Bezug Auf die Handlung, den Gegenstand oder die jeweilige Person!!!
Timbo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 166



BeitragVerfasst: So 06.08.06 00:43 
Also ich brauch die Compare-Funktion für das sortieren mit VirtualTree, da muss man die Compare-Funktion selbst implementieren...
Timbo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 166



BeitragVerfasst: So 06.08.06 00:45 
soooo, hat mir keine Ruhe gelassen.
Hab jetzt was selbst entwickelt, funzt prima.

Frage: Kann da jemand noch was an Performance raus holen, mit Codeoptimiering?
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:
23:
24:
function GetFirstZahlStr(var RestStr: String):String;
// var RestStr, ist anfangs der InStr und wird dann mit dem Rest nach der
// AnfangsZahl belegt, damit der Rest später auch noch verglichen werden kann
var
  i, FoundKommaPoint, lengthStr: Integer;
begin
  result:= '';
  lengthStr:= length(RestStr);
  if lengthStr > 0 then begin
    FoundKommaPoint:= 0;
    i:= 1;
    while (RestStr[i] in ['1','2','3','4','5','6','7','8','9','0',',','.'])
      and (FoundKommaPoint < 2and (i <= lengthStr) do begin
      if RestStr[i] in [',','.'then inc(FoundKommaPoint);
      inc(i);
    end;
    // damit letztes Komma nicht mit in result kopiert wird
    if FoundKommaPoint > 1 then dec(i);
    result:= copy(RestStr,1,i-1);
    RestStr:= copy(RestStr,i,lengthStr-i+1);
    // damit in Float umgewandelt werden kann
    result:= StringReplace(result,'.',',',[]);
  end;
end;
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:
23:
24:
25:
26:
27:
28:
29:
30:
function MyCompareText(Str1, Str2: String): Integer;
var
  ResultCompareValue: Integer;
  ZahlStr1,ZahlStr2: String;
begin
  ZahlStr1:=GetFirstZahlStr(Str1);
  ZahlStr2:=GetFirstZahlStr(Str2);

  // Keine Anfangszahl gefunden
  if (ZahlStr1 = ''and (ZahlStr2 = ''then
    result:= CompareText(Str1, Str2)
  else
    // Beides Zahlen
    if (ZahlStr1 <> ''and (ZahlStr2 <> ''then begin
      ResultCompareValue:= CompareValue(StrToFloat(ZahlStr1),
                                        StrToFloat(ZahlStr2),
                                        0.01); // Genauigkeit
      // Zahlen gleich, Rest vergleichen
      if ResultCompareValue = 0 then
        result:= CompareText(Str1, Str2)
      else
        result:= ResultCompareValue;
    end else
      // Str2 ist eine Zahl
      if ZahlStr1 = '' then
        result:= 1
      // Str1 ist eine Zahl
      else
        result:= -1;
end;