Delphi 选择排序似乎向后排序

Delphi Selection Sort seems to sort backwards

procedure TfrmSorting.btnSortClick(Sender: TObject);
var
K,L,I,iNumElements : integer;
sKeep : string;
begin
iNumElements := length(arrNames);
for K := 1 to iNumElements - 1 do
 begin
  for L := K + 1 to iNumElements do
    begin
      if arrNames[K] < arrNames[L] then
     begin
      sKeep := arrNames[L];
      arrNames[L] := arrNames[K];
      arrNames[K] := sKeep;
  end;
 end;
end;
reditNames.Lines.Clear;
I := 1;
for K := 1 to iNumElements - 1 do
  begin
    reditNames.Lines.Add(arrNames[I]);
    I := I + 1;
  end;
end;

我正在使用此排序算法对数组进行排序。然后我在 richedit 上显示内容,而不是从 A..Z 开始显示 Z..A.算法或我向 richedit 添加行的方式有问题吗?谢谢

K 小于 L,如果第 K 个小于第 L 个,则交换项目。你的比较方式是错误的。使用 > 而不是 <

您的索引也有问题。你的数组真的是从 1 开始的吗?为什么只将 1 到 N-1 添加到输出中?您是否缺少最后一项?换句话说,我怀疑您的代码中还有其他缺陷。你还没有展示这一切,所以我不能确定。

最后,为什么不使用内置的排序功能呢?