home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / pascal / 7488 < prev    next >
Encoding:
Text File  |  1992-12-15  |  1.6 KB  |  48 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!utcsri!torn!news.ccs.queensu.ca!mast.queensu.ca!dmurdoch
  3. From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
  4. Subject: Re: Wanted: Code for sorting a file
  5. Message-ID: <dmurdoch.364.724437122@mast.queensu.ca>
  6. Lines: 36
  7. Sender: news@knot.ccs.queensu.ca (Netnews control)
  8. Organization: Queen's University
  9. References: <1992Dec15.135354.10665@htsa.aha.nl>
  10. Date: Tue, 15 Dec 1992 16:32:03 GMT
  11.  
  12. In article <1992Dec15.135354.10665@htsa.aha.nl> quintenu@htsa.aha.nl (Quinten Uijldert) writes:
  13. >Who can E-Mail me some code for sorting a file. The file contains
  14. >records like this:
  15. >
  16. >TYPE
  17. >  RecType = RECORD
  18. >    Name, Street, Zip, Telephone: String;
  19. >  END;
  20. >  FileType = TEXT;
  21. >
  22. >VAR
  23. >  Rec: RecType;
  24. >  f: FileType;
  25. >
  26. >I Want to sort the file on Rec.Name, but I can't figure it out how
  27. >to do it.
  28. >I Use TP 6.0.
  29.  
  30. You don't say how big the file is.  If it's small enough to load the whole 
  31. thing into memory, the easiest way is as follows:
  32.  
  33.  1. convert your RecType to an object type, 
  34.  2. create a TStringCollection descendant whose KeyOf method returns a 
  35. pointer to the Name field
  36.  3. read your data file, and insert all the records into your collection
  37.  4. write the whole collection back to the file.
  38.  
  39. This isn't the fastest way to sort, but for a file small enough to fit into 
  40. memory (less than 16380 records, 300K total size) you probably wouldn't 
  41. notice any improvements you could make.
  42.  
  43. For a bigger file, I'd use an external sort package; QSORT (shareware by Ben 
  44. Baker available at lots of ftp sites) works well for me.
  45.  
  46. Duncan Murdoch
  47. dmurdoch@mast.queensu.ca
  48.