sorting - Delphi 按 ItemData.Detail 对 TListBox 进行排序?

标签 sorting delphi tlistbox

我有一个 TListBox,其中包含位置列表(每个位置都有一个名称和距当前位置的距离)。我想为用户提供按位置名称(即按字母顺序)或按距当前位置的距离对列表进行排序的选项。位置名称存储为项目的 ItemData.Text 值,距当前位置的距离存储为 ItemData.Detail 值。问题在于常规 TListBox 排序方法在排序时不使用 ItemData.Detail 属性(仅使用 ItemData.Text 属性)。是否可以向 TListBox 添加自定义排序方法,根据每个项目的 ItemData.Detail 值进行排序?

我尝试了以下方法,但不起作用:

procedure TFrmSelect.btnSortLocationClick(Sender: TObject);
var Compare: TFMXObjectSortCompare;
begin
  btnSortLocation.Enabled := False;
  btnSortAlpha.Enabled := True;
  Compare := function(item1, item2: TFmxObject): Integer
  begin
    Result := TListBoxItem(item1).ItemData.Detail.CompareTo(TListBoxItem(item2).ItemData.Detail);
  end;
  self.ListBox.Sort(Compare);
  self.ListBox.Sorted := False;
  self.ListBox.Sorted := True;
end;

下面是要排序的示例列表的图像:

Here is an image of an example list that would be sorted

最佳答案

Sort 的调用使用比较函数执行排序。 Sorted 属性用于按照默认比较确定的顺序维护列表。

为了使用比较函数对列表进行排序,只需删除设置 Sorted 属性的代码即可。

关于sorting - Delphi 按 ItemData.Detail 对 TListBox 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43488140/

相关文章:

python - Python 中列表的 n 个最大值

arrays - 具有 O(n) 次反转的数组

algorithm - Heapsort可以应用于最小堆数据结构吗?

delphi - 使用delphi使用一种软件在多个用户之间发送数据

multithreading - TThread 在 Delphi 2006 控制台应用程序中的工作方式是否不同?

delphi - Firemonkey (Delphi) TListbox 与 MultiSelect 获取选定项目

c++ - boost 排序和 OpenCL 缓冲区

delphi - 有谁有一个关于如何使用 Synapse 的 Heartbeat 功能的好例子吗?

Delphi:绘制抗锯齿三角形