Solved

Function to Sort Alphabetically raws of text string from a text file.

Posted on 2004-03-30
15
296 Views
Last Modified: 2010-04-05
I'm writing Delphi 7 program to sort text string data from the text file as follows:-

 CSRLB  
 MOIPHI  
 HLUAP2  
 MOIPHO  
 SSI
 C7ITTH4
 HRRLAP3
 HSSPWAP
 TONE    
 TIMER
 HNMSPAP
 C7OTHDK
 C7OTGBE
 MUEP    
 MJEC
 ASDH    
 ETGB4IF
 UPDC    
 MSPUAP  
 BL2
 ETGB3IF
 MSMOAP  
 MSMO    
 MHOC    
 DQCCH
 MHIC    
 MSMT    
 C7OGATE
 BL      
 HASCAP2
 DROP    
 ECDH    

I would like to sort the above text data into

ASDH    
BL
BL2
CSRLB
etc

Thanks
Hasim Hj. Abdul Hamid
0
Comment
Question by:Hasim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 10720187
if you use a TStringList variable, you can read the file directly into the variable and invoke the variable's .Sort method.
0
 

Author Comment

by:Hasim
ID: 10720252
Thank for your comments.

Unfortunately, I'am not using the TStringList.
I needs the sorted text data in a text file so that I can add command in front of each text string as follows:-

Sorted data in the text file
ASDH
BL
etc

Final Text File after appending SAAEP:BLOCK= in front of each
SAAEP:BLOCK=ASDH;
SAAEP:BLOCK=BL;
etc

Appreciate your further help.

Thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 10720441
You could loop through the TStringList items and output them with the prepended string 'SAAEP:BLOCK=' and appended string ';'

or...
loop through the items in the .Strings[#] property, prepending and appending the strings (see above).  Use the .SaveToFile method to save the changed strings.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:aikimark
ID: 10720471
Sorry to concentrate on the TStringList, but it seems the most logical, efficient, and fastest solution.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10720677
Hasim,

First, you need to explain more, cause no one can guess what you are doing.

Are you laoding the file already into delphi, if so, are you loading it into a control (Example - TMemo)?

If not, are you loading it into a data container, list, array, etc.

If you aren't loading it into delphi, does a file exist on hardrive and you want to sort there?

Please explain exactly what you are doing and with what or with what you what to do it with.

Shane
0
 
LVL 2

Expert Comment

by:Ratje
ID: 10721618
Don't give me the points, just providing a simple example of the answer given here allready:

procedure TForm1.Button1Click(Sender: TObject);
var
  sList : TStringlist ;
  I : Integer ;
begin
  memo1.Lines.LoadFromFile('c:\temp\test.txt') ;
  sList := Tstringlist.create();
  for I := 0 to Memo1.Lines.Count do
   sList.Add(memo1.Lines[I]) ;
  memo1.Clear ;
  sList.Sort;
  for I := 1 to sList.Count -1  do
    memo1.lines.Add('SAAEP:BLOCK='+sList.Strings[I]) ;

  freeAndNil(sList) ;
end;

All that remains is saving the data again.


Rat
0
 

Expert Comment

by:d1kGroup
ID: 10721756
you can use a TStringList only to sort. try this

procedure SortTextFile(FileName: String);
var
  list: TStringList;
begin
  list := TStringList.Create;
  list.LoadFromFile(FileName);
  list.Sort;
  list.SaveToFile(FileName);
  list.Free;
end;
0
 
LVL 45

Accepted Solution

by:
aikimark earned 125 total points
ID: 10723171
d1kGroup,

<...only to sort...>
Really?  What about:

procedure SortTextFile(FileName: String);
var
  sList: TStringList;
  nLoop: Integer;
begin
  sList:= TStringList.Create;
  sList.LoadFromFile(FileName);
  sList.Sort;
  for nLoop := 1 to sList.Count -1  do
     sList.Strings[nLoop] := 'SAAEP:BLOCK=' + sList.Strings[nLoop] + ';'
  sList.SaveToFile(FileName);
  sList.Free;
end;
0
 
LVL 45

Expert Comment

by:aikimark
ID: 10723263
oops, that should have read:
...
  for nLoop := 0 to sList.Count -1  do
...
0
 
LVL 45

Expert Comment

by:aikimark
ID: 10742677
Hasim, are you still with us?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 10765002
Hasim?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 10855719
Hasim?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question