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’
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.
Now you need to hold an array of
TPerson.In order to perform a sort you need to be able to compare two items, and swap them.
Now we can put this all together with a bubble sort.
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.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.