tags:

views:

216

answers:

3

I have read a text file (of names) into an array and I need how to sort those names into alphabetical order and display that in a rich edit?

Please give me the code from this point onwards:

readln(myfile,arr[i]);

'myfile' is the text file and 'arr' is the array of string. Also, I have declared 'i' as an integer even though it is a array of string. Is that OK?

+2  A: 

Use a StringList and then it's easy. StringList can also sort automatically. See an example here (scroll all the way down): http://www.delphibasics.co.uk/Article.asp?Name=Files

Chris Thornton
+6  A: 

Use a TStringList instead of a array and set the Sort property to true.

var
  sortlist : TStringList;            // Define our string list variable
begin
  // Define a string list object, and point our variable at it
  sortlist := TStringList.Create;
  try
    // Now add some names to our list
    sortlist.Sorted := True;

    // And now find Brian's age
    sortlist.LoadFromFile(myfile);

    // Do something.
  finally    
    // Free up the list object
    sortlist.Free;
  end;
end;
B Thomsen
names.free should be sortlist.free, and you should have a try..finally to protect the object.
Chris Thornton
Always try ... finally!
Andreas Rejbrand
This assumes that the names are the ONLY contents in the file, which may or may not be a correct assumption.
Deltics
+1  A: 
Jørn E. Angeltveit