Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting of Arrays Alphabetically?

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.

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.

Example of scenario:

Smith           25 
Appleseed       32
Gibbs           45

Must turn into:

Appleseed       32
Gibbs           45
Smith           25

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

UPDATE: I switched to record.

Tried this code with no avail

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'

like image 332
noob Avatar asked Nov 29 '22 10:11

noob


1 Answers

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;

Now you need to hold an array of TPerson.

type
  TPersonArray = array of TPerson;

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.

like image 75
David Heffernan Avatar answered Dec 09 '22 15:12

David Heffernan