数组按字母顺序排序? [英] Sorting of Arrays Alphabetically?

查看:364
本文介绍了数组按字母顺序排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个字符串数组,分别名为 arrayone和 arraytwo
我该如何继续按字母顺序对 arrayone进行排序(从A到Z),同时仍保持与第二个数组的关系。

Say I have two arrays of string, named 'arrayone' and 'arraytwo' How would I go about sorting the 'arrayone' alphabetically (from A to Z), while still keeping relations to my second array.

如果您想知道'arrayone'和'arraytwo'中的内容,每个人的年龄分别为1和2。我的最终结果是将其添加到richedit。

Incase you were wondering what is in 'arrayone' and 'arraytwo', 1 has surnames and 2 has the ages of each person. My end result is to add it to a richedit.

示例示例:

Smith           25 
Appleseed       32
Gibbs           45

必须变成:

Appleseed       32
Gibbs           45
Smith           25

请不要使用字符串列表,将其保留在简单的数组和过程中。

Please no stringlist, keep it in simple array and in a procedure.

更新:我

尝试此代码无济于事

for i := 0 to 26 do
for j := 0 to 26 do
  if recordname.surname[j] > recordname.surname[j+1] then begin
    line := recordname.surname[j];
    line[j] := recordname.surname[j+1];
    recordname.surname[j+1] := line;
  end;

表示不兼容的类型:字符和字符串

It says Incompatible Types: 'Char' and 'String'

推荐答案

在向您提供有关数据结构的建议,并看到了随之而来的斗争之后,我想直截了当,并更清楚地解释我的意思。

Having given you advice about your data structure, and seen the ensuing struggles, I want to put things straight and explain more clearly what I mean.

您的原始代码具有两个基本上未连接的数组。您可以交换一个阵列中的项目,而轻易忘记为另一个阵列进行交换。在我看来,姓名/年龄对确实不应分开。这将导致以下类型声明。

You original code had two arrays that were essentially unconnected. You could swap items in one array and easily forget to do so for the other array. It looks to me like the name/age pairs really should not be split apart. This leads to the following type declaration.

type
  TPerson = record
    Name: string;
    Age: Integer;
  end;

现在您需要持有一组 TPerson

Now you need to hold an array of TPerson.

type
  TPersonArray = array of TPerson;

要执行排序,您需要能够比较两个项目并交换它们。 / p>

In order to perform a sort you need to be able to compare two items, and swap them.

function Compare(const Person1, Person2: TPerson): Integer;
begin
  Result := CompareText(Person1.Name, Person2.Name);
end;

procedure Swap(var Person1, Person2: TPerson);
var
  temp: TPerson;
begin
  temp := Person1;
  Person1 := Person2;
  Person2 := temp;
end;

现在我们可以将所有这些与冒泡排序结合起来。

Now we can put this all together with a bubble sort.

procedure Sort(var People: TPersonArray);
var
  i, n: Integer;
  Swapped: Boolean;
begin
  n := Length(People);
  repeat
    Swapped := False;
    for i := 1 to n-1 do begin
      if Compare(People[i-1], People[i])>0 then begin
        Swap(People[i-1], People[i]);
        Swapped := True;
      end;
    end;
    dec(n);
  until not Swapped;
end;

现在,如果您想使用更复杂的比较运算符,则只需替换比较。例如,如果要按年龄订购具有相同姓名的任何人,则可以使用词典比较功能。

Now, if you wanted to use a more complex comparison operator then you could simply replace Compare. For example, if you wanted to order by age any people that have the same name, then you use a lexicographic comparison function.

function Compare(const Person1, Person2: TPerson): Integer;
begin
  Result := CompareText(Person1.Name, Person2.Name);
  if Result=0 then begin
    Result := Person2.Age-Person1.Age;
  end;
end;

我已经一步一步地写了这个答案,这就是你应该如何解决这样一个更大的问题。尝试将其分解为较小的部分,每个部分都是可管理的。

I have written this answer piece by piece and that is how you should approach a larger problem like this. Try to break it down in to smaller pieces, each of which is manageable.

这篇关于数组按字母顺序排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆