how do i sort by alphabet...

i have a txt file and i read this file line by line...

the text file look like:

Code:
test\test.txt
newtest\newtest.txt
anothertest\text.bat
newtest\newtest.txt
now i want to sort the lines by alphabet and write it back to the file in the right order so that it will look like:

Code:
anothertest\text.bat
newtest\newtest.txt
test\test.txt
so at first i need to sort the first entry (delimiter "\" )
then the second entry...
and so on

and if there is a entry twice in the list, i want to delete one

i have no idea how to do this in progress =/

thank you for your help
 

GregTomkins

Active Member
I'm sure there are several ways but personally I would do one of these:

1) Use the Unix sort utility, perhaps with the -u remove duplicates parameter; or, if I wanted to be OS-independent, which I rarely do:

2) Load the text into a temp-table, write some code to find duplicates, then FOR EACH BY it to sort.
 
Code:
OUTPUT STREAM gsFileOutput TO VALUE(lcOutputPath).

FOR EACH ltOutPutText NO-LOCK BY ltOutPutText.lcText.
   EXPORT STREAM gsFileOutput ltOutPutText.lcText.
END.

OUTPUT STREAM gsFileOutput CLOSE.
when i export my text with this code i geht a " at the start and the end of each line =/
 

enoon

Member
Export users quotes to represent string datatype. So the solution would be to build it manually if you wish any other format.
With PUT statement.

PUT UNFORMATTED ttTable.ttField " " ttTable.ttField2 SKIP.

and so on...
 
This may not be an issue, but bear in mind any basic 4GL sorting will render the list in ascii order, and will not (for instance) handle embedded numbers in the human-friendly way that Windows does.

eg.

order of the following filenames would be:

file1
file12
file2

not

file1
file2
file12
 

4GLNewbie

Member
That is correct in alfabetical order.

Take this example:
Files
File
Filf

The correct alphabetical order is:
File
Files
Filf

Exactly what your sorting procedure does.
 

StuartT

Member
i have a txt file and i read this file line by line...

the text file look like:

Code:
test\test.txt
newtest\newtest.txt
anothertest\text.bat
newtest\newtest.txt
now i want to sort the lines by alphabet and write it back to the file in the right order so that it will look like:

Code:
anothertest\text.bat
newtest\newtest.txt
test\test.txt
so at first i need to sort the first entry (delimiter "\" )
then the second entry...
and so on

and if there is a entry twice in the list, i want to delete one

i have no idea how to do this in progress =/

thank you for your help

This shoud do what you need.

Code:
  def var cname as char no-undo.
  def temp-table tt-filesort
    field tt-name
    index i-main tt-name asc.
  input from <input file name> no-echo.
  repeat:
     set cname.
     find tt-filesort where
           tt-filesort.tt-name = cname no-error.
     if not avail tt-filesort then
     do:
        create tt-filesort.
        assign tt-filesort.tt-name = cname.
     end.
  end.
  input close.
  output to <sorted file without duplicates>.
  for each tt-filesort:
     put unformatted tt-filesort.tt-name skip.
  end.
  output close.
 
That is correct in alfabetical order.

Take this example:
Files
File
Filf

The correct alphabetical order is:
File
Files
Filf

Exactly what your sorting procedure does.

Assuming you were responding to me, it is in correct order (as I said), but not in the more friendly order that users are used to seeing in say, Windows when numbers are taken into consideration.

If you look at a Windows listiing of directory contents with numbers like the example I gave, you will see they are not in ascii order, despite clicking on the "sort by Name" column.
 
Top