![]() |
Nach Alphabet sortieren (ValueListEditor)
Hi,
wie der Name schon sagt: Wie kann ich die Einträge einer ValueList nach Alphabet sortieren? |
Re: Nach Alphabet sortieren (ValueListEditor)
Ein Hallöle von
![]() Die Einfache Variante:
Delphi-Quellcode:
ValueListEditor1.Strings[...] enthält eine Kombination aus Schlüssel und Wert ("Schlüssel=Wert") und wenn genauer sortiert werden soll, muß der String geteilt werden.
Var I, I2: Integer;
S: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do If ValueListEditor1.Strings[I] > ValueListEditor1.Strings[I2] Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; Nach Schlüssel:
Delphi-Quellcode:
Nach Wert:
Var I, I2: Integer;
S, S2: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do Begin S := Copy(ValueListEditor1.Strings[I], 1, Pos('=', ValueListEditor1.Strings[I] - 1); S2 := Copy(ValueListEditor1.Strings[I2], 1, Pos('=', ValueListEditor1.Strings[I2] - 1); If S > S2 Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; End;
Delphi-Quellcode:
Oder mal eine Hochgenaue Variante for Schlüssel
Var I, I2: Integer;
S, S2: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do Begin S := Copy(ValueListEditor1.Strings[I], Pos('=', ValueListEditor1.Strings[I] + 1, Length(ValueListEditor1.Strings[I])); S2 := Copy(ValueListEditor1.Strings[I2], Pos('=', ValueListEditor1.Strings[I2] + 1, Length(ValueListEditor1.Strings[I2])); If S > S2 Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; End;
Delphi-Quellcode:
Und noch mal für Wert:
Var I, I2: Integer;
S, S2, S3, S4: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do Begin S := Copy(ValueListEditor1.Strings[I], 1, Pos('=', ValueListEditor1.Strings[I] - 1); S2 := Copy(ValueListEditor1.Strings[I], Pos('=', ValueListEditor1.Strings[I] + 1, Length(ValueListEditor1.Strings[I])); S3 := Copy(ValueListEditor1.Strings[I2], 1, Pos('=', ValueListEditor1.Strings[I2] - 1); S4 := Copy(ValueListEditor1.Strings[I2], Pos('=', ValueListEditor1.Strings[I2] + 1, Length(ValueListEditor1.Strings[I2])); While Length(S) < Length(S3) do S := S + ' '; While Length(S3) < Length(S) do S3 := S3 + ' '; While Length(S2) < Length(S4) do S2 := S2 + ' '; While Length(S4) < Length(S2) do S4 := S4 + ' '; If (S > S3) or ((S = S3) and (S2 > S4)) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; End;
Delphi-Quellcode:
http://www.delphipraxis.net/images/common/divider.jpg
Var I, I2, I3: Integer;
S, S2, S3, S4: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do Begin S := Copy(ValueListEditor1.Strings[I], 1, Pos('=', ValueListEditor1.Strings[I] - 1); S2 := Copy(ValueListEditor1.Strings[I], Pos('=', ValueListEditor1.Strings[I] + 1, Length(ValueListEditor1.Strings[I])); S3 := Copy(ValueListEditor1.Strings[I2], 1, Pos('=', ValueListEditor1.Strings[I2] - 1); S4 := Copy(ValueListEditor1.Strings[I2], Pos('=', ValueListEditor1.Strings[I2] + 1, Length(ValueListEditor1.Strings[I2])); While Length(S) < Length(S3) do S := S + ' '; While Length(S3) < Length(S) do S3 := S3 + ' '; While Length(S2) < Length(S4) do S2 := S2 + ' '; While Length(S4) < Length(S2) do S4 := S4 + ' '; If (S2 > S4) or ((S2 = S4) and (S > S3)) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; End; ![]() ![]() ![]() |
Re: Nach Alphabet sortieren (ValueListEditor)
:shock: *plattis*
Das ist eigentlich "Code-Lobrary-Reif" Naja, ich denke was du sagen willst. Ich bin sehr sehr erfreut und es funzt einwandtfrei. Vielen Dank für deine Bemühungen ;-) |
Re: Nach Alphabet sortieren (ValueListEditor)
Nur der Vollstängkeit halber: Die Namen / Werte lassen sich auch ohne umständliches Copy() ermitteln:
Delphi-Quellcode:
bzw.
ValueListEditor1.Strings.Names[I]
Delphi-Quellcode:
Oder die Kombination, um einen Wert per Index zu bekommen:
ValueListEditor1.Strings.Values['Name'];
Delphi-Quellcode:
ValueListEditor1.Strings.Values[ValueListEditor1.Strings.Names[I]]
|
Re: Nach Alphabet sortieren (ValueListEditor)
Noch ein Hallöle von
![]() hatte mit ValueListEditor noch nicht so viel zu tun. Also jetzt noch mal überarbeitet. Hab sogar noch eine Verbesserung gefunden:
Delphi-Quellcode:
Nach Schlüsseln in aufsteigender Reihfolge (A => Z):
ValueListEditor1.Strings.Values[ValueListEditor1.Strings.Names[I]]
{=} ValueListEditor1.Strings.ValueFromIndex[I]
Delphi-Quellcode:
Nach Werten in aufsteigender Reihfolge (A => Z):
Var I, I2: Integer;
S: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do If (ValueListEditor1.Strings.Names[I] > ValueListEditor1.Strings.Names[I2]) or ((ValueListEditor1.Strings.Names[I] = ValueListEditor1.Strings.Names[I2]) and (ValueListEditor1.Strings.ValueFromIndex[I] > ValueListEditor1.Strings.ValueFromIndex[I2])) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End;
Delphi-Quellcode:
Nach Schlüsseln in fallender Reihnfolge (Z => A):
Var I, I2: Integer;
S: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do If (ValueListEditor1.Strings.ValueFromIndex[I] > ValueListEditor1.Strings.ValueFromIndex[I2]) or ((ValueListEditor1.Strings.ValueFromIndex[I] = ValueListEditor1.Strings.ValueFromIndex[I2]) and (ValueListEditor1.Strings.Names[I] > ValueListEditor1.Strings.Names[I2])) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End;
Delphi-Quellcode:
Nach Werten in fallender Reihfolge (Z => A):
Var I, I2: Integer;
S: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do If (ValueListEditor1.Strings.Names[I] < ValueListEditor1.Strings.Names[I2]) or ((ValueListEditor1.Strings.Names[I] = ValueListEditor1.Strings.Names[I2]) and (ValueListEditor1.Strings.ValueFromIndex[I] < ValueListEditor1.Strings.ValueFromIndex[I2])) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End;
Delphi-Quellcode:
http://www.delphipraxis.net/images/common/divider.jpg
Var I, I2: Integer;
S: String; For I := 0 to ValueListEditor1.Strings.Count - 2 do For I2 := I + 1 to ValueListEditor1.Strings.Count - 1 do If (ValueListEditor1.Strings.ValueFromIndex[I] < ValueListEditor1.Strings.ValueFromIndex[I2]) or ((ValueListEditor1.Strings.ValueFromIndex[I] = ValueListEditor1.Strings.ValueFromIndex[I2]) and (ValueListEditor1.Strings.Names[I] < ValueListEditor1.Strings.Names[I2])) Then Begin S := ValueListEditor1.Strings[I]; ValueListEditor1.Strings[I] := ValueListEditor1.Strings[I2]; ValueListEditor1.Strings[I2] := S; End; ![]() ![]() ![]() |
Re: Nach Alphabet sortieren (ValueListEditor)
Mal als billige alternative:
Delphi-Quellcode:
Das sortiert die Einträge jetzt aufsteigend... für Absteigend müsste man das entweder rückwärts zurückkopieren (billige Version), oder aber man müsste mit CustomSort arbeiten (der korrekte Weg).
var SortList: TStringList;
begin SortList := TStringList.Create; with ValueListEditor1 do begin SortList.Assign(Strings); SortList.Sort; Strings.Assign(SortList); end; SortList.Free; end; Gruß Wormid |
AW: Nach Alphabet sortieren (ValueListEditor)
Dieser Tip war Gold wert! Vielen Dank dafür 8-)
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:33 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz