- // Méthode 1 :
- function STRING_INTEGER(Str: String; StringRead: TStringRead): String;
- var I: Integer;
- Cont: Boolean;
- begin
- Cont := True;
- RESULT := '';
-
- if StringRead = srFromLeft
- then begin
- for i := 1 to Length(Str) do
- if (Cont) and (Str[i] in ['-', '0'..'9'])
- then begin
- if Str[i] = '-'
- then begin
- if (RESULT = '') and (i < length(Str))
- then begin
- if Str[i+1] in ['0'..'9']
- then RESULT := '-';
- end
- else
- Cont := false;
- end
- else
- RESULT := RESULT + Str[i];
- end
- else
- Cont := RESULT = '';
- end
- else begin
- for i := Length(Str) downto 1 do
- if (Cont) and (Str[i] in ['-', '0'..'9'])
- then begin
- if Str[i] = '-'
- then begin
- if RESULT <> ''
- then begin
- RESULT := '-' + RESULT;
- Cont := false;
- end;
- end
- else
- RESULT := Str[i] + RESULT;
- end
- else
- Cont := RESULT = '';
- end;
- end;
-
-
- procedure TSTRINGS_TRI(Lista: TStrings);
- var
- i, j, IndSmall, ValueSmall, ValueItem : Integer;
- Str: String;
- begin
- for i := 0 to Lista.Count - 2 do
- begin
- // Le plus petit pour l' instant :
- IndSmall := i;
- Str := STRING_INTEGER(Lista[i], srFromLeft);
-
- if Str <> '' // Partie Integer de l' item trouvé ?
- then ValueSmall := StrToInt(Str)
- else ValueSmall := 0; // On peut mettre la valeur que l' on veut selon ce que l' on veut faire ...
-
- // Rechercher le plus petit depuis la position i+1 :
- for j := i + 1 to Lista.Count - 1 do
- begin
- // Récupérer l' integer dans l' item :
- Str := STRING_INTEGER(Lista[j], srFromLeft);
-
- if Str <> '' // Partie Integer de l' item trouvé ?
- then ValueItem := StrToInt(Str)
- else ValueItem := 0; // On peut mettre la valeur que l' on veut selon ce que l' on veut faire ...
-
- if ValueItem < ValueSmall
- then begin
- IndSmall := j;
- ValueSmall := ValueItem;
- end;
- end;
-
- if IndSmall <> i // Si les indices sont différents, permuter les 2 items ...
- then begin
- Str := Lista[i];
- Lista[i] := Lista[IndSmall];
- Lista[IndSmall] := Str;
- end;
- end;
- end;
-
- procedure TForm1.BtnExec1Click(Sender: TObject);
- begin
- TSTRINGS_TRI(LB1.Items);
- end;
-
- // Méthode 2 :
- procedure TForm1.BtnExec2Click(Sender: TObject);
- type
- rItemData = Record
- Value: Integer; // Entier ou autre qui nous permettra d' ordonner
- ItemStr: String; // Texte que l' on veut passer dans la ListBox
- Exported: Boolean; // Savoir si on a déjá passé cet item dans la ListBox
- end;
- var
- NbItems, IndSmallItem: Integer;
- Array_Items: Array of rItemData;
-
- procedure ARRAY_ADD_ITEM(aValue: Integer; aItemStr: String);
- begin
- inc(NbItems, 1);
- SetLength(Array_Items, NbItems);
-
- Array_Items[NbItems-1].Exported := false;
- Array_Items[NbItems-1].Value := aValue;
- Array_Items[NbItems-1].ItemStr := aItemStr;
- end;
-
- function GetIndSmallItem: Integer;
- var i: Integer;
- begin
- RESULT := -1; // Aucun par défaut ...
-
- for i := 0 to NbItems -1 do
- if not Array_Items[i].Exported // Pas encore passé dans la ListBox ...
- then
- if RESULT = -1 // Aucun pour l'instant
- then
- RESULT := i
- else // *** Ici on compare des integers, mais on peut comparer tout ce que l' on veut *** //
- if Array_Items[i].Value < Array_Items[RESULT].Value
- then RESULT := i; // Nouveau item plus petit trouvé !!!
- end;
-
- begin
- NbItems := 0;
- LB2.Items.Clear;
-
- // Chargement des items que l' on veut ordonner dans l' array :
- ARRAY_ADD_ITEM(35, ' Mon item de valeur trente cinq');
- ARRAY_ADD_ITEM(18, ' Mon item de valeur dix huit');
- ARRAY_ADD_ITEM(25, ' Mon item de valeur vingt cinq');
- ARRAY_ADD_ITEM(3, ' Mon item de valeur trois');
- ARRAY_ADD_ITEM(999, ' Mon item de valeur neuf cent quatre vingt dix neuf');
- ARRAY_ADD_ITEM(-6, ' Mon item de valeur moins six');
- ARRAY_ADD_ITEM(25, ' Mon item de valeur vingt cinq'); // Doublon lol
-
- // Passer les items dans la ListBox :
- repeat
- IndSmallItem := GetIndSmallItem;
-
- if IndSmallItem <> -1
- then begin
- LB2.Items.Add(Array_Items[IndSmallItem].ItemStr);
- Array_Items[IndSmallItem].Exported := true;
- end;
- until IndSmallItem = -1; // Plus d' item dans l' array
- end;
// Méthode 1 :
function STRING_INTEGER(Str: String; StringRead: TStringRead): String;
var I: Integer;
Cont: Boolean;
begin
Cont := True;
RESULT := '';
if StringRead = srFromLeft
then begin
for i := 1 to Length(Str) do
if (Cont) and (Str[i] in ['-', '0'..'9'])
then begin
if Str[i] = '-'
then begin
if (RESULT = '') and (i < length(Str))
then begin
if Str[i+1] in ['0'..'9']
then RESULT := '-';
end
else
Cont := false;
end
else
RESULT := RESULT + Str[i];
end
else
Cont := RESULT = '';
end
else begin
for i := Length(Str) downto 1 do
if (Cont) and (Str[i] in ['-', '0'..'9'])
then begin
if Str[i] = '-'
then begin
if RESULT <> ''
then begin
RESULT := '-' + RESULT;
Cont := false;
end;
end
else
RESULT := Str[i] + RESULT;
end
else
Cont := RESULT = '';
end;
end;
procedure TSTRINGS_TRI(Lista: TStrings);
var
i, j, IndSmall, ValueSmall, ValueItem : Integer;
Str: String;
begin
for i := 0 to Lista.Count - 2 do
begin
// Le plus petit pour l' instant :
IndSmall := i;
Str := STRING_INTEGER(Lista[i], srFromLeft);
if Str <> '' // Partie Integer de l' item trouvé ?
then ValueSmall := StrToInt(Str)
else ValueSmall := 0; // On peut mettre la valeur que l' on veut selon ce que l' on veut faire ...
// Rechercher le plus petit depuis la position i+1 :
for j := i + 1 to Lista.Count - 1 do
begin
// Récupérer l' integer dans l' item :
Str := STRING_INTEGER(Lista[j], srFromLeft);
if Str <> '' // Partie Integer de l' item trouvé ?
then ValueItem := StrToInt(Str)
else ValueItem := 0; // On peut mettre la valeur que l' on veut selon ce que l' on veut faire ...
if ValueItem < ValueSmall
then begin
IndSmall := j;
ValueSmall := ValueItem;
end;
end;
if IndSmall <> i // Si les indices sont différents, permuter les 2 items ...
then begin
Str := Lista[i];
Lista[i] := Lista[IndSmall];
Lista[IndSmall] := Str;
end;
end;
end;
procedure TForm1.BtnExec1Click(Sender: TObject);
begin
TSTRINGS_TRI(LB1.Items);
end;
// Méthode 2 :
procedure TForm1.BtnExec2Click(Sender: TObject);
type
rItemData = Record
Value: Integer; // Entier ou autre qui nous permettra d' ordonner
ItemStr: String; // Texte que l' on veut passer dans la ListBox
Exported: Boolean; // Savoir si on a déjá passé cet item dans la ListBox
end;
var
NbItems, IndSmallItem: Integer;
Array_Items: Array of rItemData;
procedure ARRAY_ADD_ITEM(aValue: Integer; aItemStr: String);
begin
inc(NbItems, 1);
SetLength(Array_Items, NbItems);
Array_Items[NbItems-1].Exported := false;
Array_Items[NbItems-1].Value := aValue;
Array_Items[NbItems-1].ItemStr := aItemStr;
end;
function GetIndSmallItem: Integer;
var i: Integer;
begin
RESULT := -1; // Aucun par défaut ...
for i := 0 to NbItems -1 do
if not Array_Items[i].Exported // Pas encore passé dans la ListBox ...
then
if RESULT = -1 // Aucun pour l'instant
then
RESULT := i
else // *** Ici on compare des integers, mais on peut comparer tout ce que l' on veut *** //
if Array_Items[i].Value < Array_Items[RESULT].Value
then RESULT := i; // Nouveau item plus petit trouvé !!!
end;
begin
NbItems := 0;
LB2.Items.Clear;
// Chargement des items que l' on veut ordonner dans l' array :
ARRAY_ADD_ITEM(35, ' Mon item de valeur trente cinq');
ARRAY_ADD_ITEM(18, ' Mon item de valeur dix huit');
ARRAY_ADD_ITEM(25, ' Mon item de valeur vingt cinq');
ARRAY_ADD_ITEM(3, ' Mon item de valeur trois');
ARRAY_ADD_ITEM(999, ' Mon item de valeur neuf cent quatre vingt dix neuf');
ARRAY_ADD_ITEM(-6, ' Mon item de valeur moins six');
ARRAY_ADD_ITEM(25, ' Mon item de valeur vingt cinq'); // Doublon lol
// Passer les items dans la ListBox :
repeat
IndSmallItem := GetIndSmallItem;
if IndSmallItem <> -1
then begin
LB2.Items.Add(Array_Items[IndSmallItem].ItemStr);
Array_Items[IndSmallItem].Exported := true;
end;
until IndSmallItem = -1; // Plus d' item dans l' array
end;