this slowpoke moves

Sort Numbers & Seperators

var
  Form1: TForm1;
  n1: Double;
  { used to assign converted numbers to double length integers, before conversion }
  n2: Double;     { n1 = first number in pair, n2 = second number in pair }

const
  sThouSep = ',';
  { defines the thousand separator being used in TListView - change if required }
  
//

{ main sorting function - convert strings to numbers, then sort accordingly }
function CustomSizeSortProc(Item1, Item2: TListItem; ParamSort: Integer): Integer;
  stdcall;
begin
  n1 := 0;
  n2 := 0;

  { string conversion and assignment process to n1 or n2, based on order being sorted }
  if ParamSort = 1 then
  begin
    n1 := StrToFloat(StringReplace(Item1.SubItems.Strings[0], sThouSep, '',
      [rfReplaceAll]));
    n2 := StrToFloat(StringReplace(Item2.SubItems.Strings[0], sThouSep, '',
      [rfReplaceAll]));
  end
  else if ParamSort = -1 then
  begin
    n1 := StrToFloat(StringReplace(Item2.SubItems.Strings[0], sThouSep, '',
      [rfReplaceAll]));
    n2 := StrToFloat(StringReplace(Item1.SubItems.Strings[0], sThouSep, '',
      [rfReplaceAll]));
  end;

  { determines final position, based on comparing results from conversion process }
  if n1 > n2 then Result := 1 
  else if n1 < n2 then Result := -1 
  else 
    Result := 0;
end;

{ determine direction of sort, then call sort function }
{ can be called from other components, such as a TButton }
procedure TForm1.Button1Click(Sender: TObject);
begin
  if ListView1.Tag = -1 then ListView1.Tag := 1 
  else 
    ListView1.Tag := -1;
  ListView1.CustomSort(@CustomSizeSortProc, ListView1.Tag);
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  if column = ListView1.Column[1] then Button1Click(Sender);
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate