Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

HELP! Sorting array!

ok this might seem odd but i'm one of those strange people who can do sum hard stuff but sum simple stuff i just cant do! :D

Ok, i have an array of records of members to a club. In this format:

Member= record
forename,surname : string;
Sorted : boolean;
MemberChoices: array[0..6] of integer;
end;

Ignore 'sorted' and 'Memberchoices'. All i want to do is sort this array into alphabetical order by their surname.

Thanks a lot for any info/code ;)

Josh

Comments

  • zibadianzibadian Member Posts: 6,349
    : ok this might seem odd but i'm one of those strange people who can do sum hard stuff but sum simple stuff i just cant do! :D
    :
    : Ok, i have an array of records of members to a club. In this format:
    :
    : Member= record
    : forename,surname : string;
    : Sorted : boolean;
    : MemberChoices: array[0..6] of integer;
    : end;
    :
    : Ignore 'sorted' and 'Memberchoices'. All i want to do is sort this array into alphabetical order by their surname.
    :
    : Thanks a lot for any info/code ;)
    :
    : Josh
    :
    You can use a simple bubble sort for that:
    [code]
    for i := 0 to MemberArrayCount-1 do
    for j := 0 to MemberArrayCount-2 do
    if MemberArray[j].Surname > MemberArray[j+1].Surname then begin
    // If members need to swap, then swap the records
    TempMember := MemberArray[j];
    MemberArray[j] := MemberArray[j+1];
    MemberArray[j+1] := TempMember;
    end;
    [/code]
    You'll need to change the variable names to match the rest of your code.
    This is the slowest sorting method, but it is the easiest to write. If you need faster sorting codes, loop up the selection sort and quick sort.
    Another method is the insertion sort, where each record is not added to the end of the list, but inserted based on its (in your case)alphabetical position.
  • mudfoxmudfox Member Posts: 5
    woohoo! i worked this out like 10 mins after i posted! but thankyou for the clafication and help. :)
Sign In or Register to comment.