按用户定义的标准对列表框进行排序 - delphi xe6

时间:2014-08-07 20:36:19

标签: ios delphi listbox delphi-xe6

我正在尝试为Listbox实现自己的排序功能。我想要做的是,根据所选标准对列表框中的项目进行排序,无论是基于例如criteria1,criteria2,criteria3 ......等等。

我有这些,让我们称它们为randomObjects,作为数据的记录。 这些随机对象如下所示:

type
  randomObject = class(TObject)
    ID,
    criteria1,
    criteria2,
    criteria3:String;
  end;

这是我想要在列表框中对randomObject进行排序时调用的函数:

procedure TmyForm.sortME(criteria: string; asc: Boolean);
var
  tempList,pList:TStringList;
  p:part;
  i:integer;
  item:TListBoxItem;
begin
  tempList := TStringList.Create;
  for p in AllRandomObjects do //allRandomObjects is an array of RandomObject
    if criteria = 'Criteria1' then
      tempList.Append(p.Criteria1)
    else if criteria = 'Criteria2' then
      tempList.Append(p.Criteria2)
    else if criteria = 'Criteria3' then
      tempList.Append(p.criteria3);

  Plist:= TStringList.Create; //pList to keep track of original order to reference the RandomObjects
  plist.Text := tempList.Text;
  if asc then
    tempList.Sorted:=true //create new order
  else
    tempList.CustomSort(StringListSortCompare); //create new order

  listbox.BeginUpdate;
  listbox.Items.Clear;
  for i := 0 to tempList.Count-1 do
  begin
    p := AllRandomObjects[plist.IndexOf(tempList.Strings[i])];
    Listbox.Items.Append('');
    item := Listbox.ListItems[Listbox.Items.Count-1];
    //blah-blah, create text objects to add to the item for displaying
    end;
  end;
  listbox.EndUpdate;

end;

这需要很长时间(至少几秒钟,在移动设备上似乎永远)。 只是学习,我确信有更好/更快/更有效的方法。请帮忙或给予指导!谢谢!为iOS开发,FMX Delphi xe6

1 个答案:

答案 0 :(得分:1)

您应该使用System.Generics.Collections.TArray.Sort对数组本身进行排序,然后遍历数组以将数据填充到列表框中。

uses
  System.Generics.Collections,
  System.Generics.Defaults,
  System.StrUtils;

procedure TmyForm.sortME( criteria: string; asc: Boolean );
var
  LCriteriaIndex : Integer;
  LData : TList<randomObject>;
begin
  LCriteriaIndex := IndexText( criteria, ['criteria1', 'criteria2', 'criteria3'] );

  if LCriteriaIndex < 0 then
    raise EArgumentException.Create( 'unknown criteria' );

  LData := TList<randomObject>.Create( TComparer<randomObject>.Construct(
        function( const L, R : randomObject ) : Integer
    begin
      case LCriteriaIndex of
        0 :
          Result := CompareText( L.criteria1, R.criteria1 ); 
        1 :
          Result := CompareText( L.criteria2, R.criteria2 );
        2 :
          Result := CompareText( L.criteria3, R.criteria3 );
      end;

      if not asc
      then
        Result := -Result;
    end ) );

  LData.AddRange( AllRandomObjects );
  LData.Sort;

  showData( LData.ToArray );
end;

procedure TmyForm.showData( AData : TArray<randomObject> );
var
  LData : randomObject;
  LItem : TListBoxItem;
begin
  listbox.BeginUpdate;
  try
    listbox.Clear;
    for LData in AData do
    begin
      LItem := TListBoxItem.Create( listbox );

      //blah-blah, create text objects to add to the item for displaying

      listbox.AddObject( LItem );
    end;
  finally
    listbox.EndUpdate;
  end;
end;