var Sens: boolean; //sens du tri. Inversé à chaque clic sur le titre d'une colonne {Compare Int1 et Int 2. Renvoie : -1 si Int1 < Int2, 0 si Int1 = Int2 et 1 si Int1 > Int2} function IsGreater(const Int1, Int2: integer):integer; begin if Int1 > Int2 then Result := 1 else if Int1 < Int2 then Result := -1 else Result := 0; end; function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall; begin case ParamSort of 0: {première colonne} begin if Sens then Result := CompareText(Item1.Caption,Item2.Caption) else Result := - CompareText(Item1.Caption,Item2.Caption); end; 1: {la colonne contient la taille des fichiers. Il ne faut donc pas comparer les valeurs d'après leur code ASCII} begin if Sens then Result := IsGreater(StrToInt(Item1.SubItems[ParamSort - 1]), StrToInt(Item2.SubItems[ParamSort - 1])) else Result := IsGreater(StrToInt(Item2.SubItems[ParamSort - 1]), StrToInt(Item1.SubItems[ParamSort - 1])); end else if Sens then Result := CompareText(Item1.SubItems[ParamSort - 1], Item2.SubItems[ParamSort - 1]) else Result := - CompareText(Item1.SubItems[ParamSort - 1], Item2.SubItems[ParamSort - 1]); end {case} end; procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin //inversion du sens de classement à chaque clic Sens := not Sens; //Appel d'une fonction callback de tri personnalisé et passage aussi de la colonne cliquée ListView1.CustomSort(@CustomSortProc, Column.Index); end;
var Sens: boolean; function CompareNum(const Int1, Int2: integer):integer;overload begin if Int1 > Int2 then Result := 1 else if Int1 < Int2 then Result := -1 else Result := 0; end; function CompareNum(const Float1, Float2: Real):integer;overload; begin if Float1 > Float2 then Result := 1 else if Float1 < Float2 then Result := -1 else Result := 0; end; function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall; begin case ParamSort of 0: {première colonne} begin if Sens then Result := CompareText(Item1.Caption,Item2.Caption) else Result := - CompareText(Item1.Caption,Item2.Caption); end; {1: la colonne contient la taille des fichiers. Il ne faut donc pas comparer les valeurs d'après leur code ASCII begin if Sens then Result := CompareNum(StrToInt(Item1.SubItems[ParamSort - 1]), StrToInt(Item2.SubItems[ParamSort - 1])) else Result := CompareNum(StrToInt(Item2.SubItems[ParamSort - 1]), StrToInt(Item1.SubItems[ParamSort - 1])); end} 1: //Idem ci-dessus mais avec des valeurs flottantes begin if Sens then Result := CompareNum(StrToFloat(Item1.SubItems[ParamSort - 1]), StrToFloat(Item2.SubItems[ParamSort - 1])) else Result := CompareNum(StrToFloat(Item2.SubItems[ParamSort - 1]), StrToFloat(Item1.SubItems[ParamSort - 1])); end else if Sens then Result := CompareText(Item1.SubItems[ParamSort - 1], Item2.SubItems[ParamSort - 1]) else Result := - CompareText(Item1.SubItems[ParamSort - 1], Item2.SubItems[ParamSort - 1]); end {case} end; procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin DecimalSeparator := '.'; Sens := not Sens; ListView1.CustomSort(@CustomSortProc, Column.Index); end;
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question