# Reorder — Re-orders a database file

When you use the Data application and enter or change an entry, it goes to the end of the database file. However, if in your address book, each entry begins with a person’s second name - for example, `Tate, Hazel` - you can use this program to re-order all of the entries. This doesn’t change the way you find an entry, but after running it you can step through it like a paper address book, or print it out neatly ordered.

This procedure can be used as required for any data file in internal memory or on memory disk. The dialog it shows is set to show data files used by Data.

You can adapt this procedure to sort other types of data files in other ways.

Note that there more advanced features than this available in the Database OPX. See About OPXs for more details of this. You could also use restriction of files by UID in the `dFILE` keyword to restrict to databases only.

        PROC reorder:
              LOCAL last%,e$(255),e%,lpos%,n$(255),c%
              n$="\dat\*.dbf"
              dINIT "Re-order Data file"
              dFILE n$,"Filename",0
              IF DIALOG                              REM returns 0 if cancelled
                    OPEN n$,a,a$
                    LAST :last%=POS
                    IF COUNT>0
                          WHILE last%<>0
                                POSITION last% :e%=POS
                                e$=UPPER$(a.a$)
                                DO
                                      IF UPPER$(a.a$)<e$
                                            e$=UPPER$(a.a$) :e%=POS
                                      ENDIF
                                      lpos%=POS :BACK
                                UNTIL pos=1 and lpos%=1
                                POSITION e%
                                PRINT e$
                                UPDATE :last%=last%-1
                          ENDWH
                    ENDIF
                    CLOSE
              ENDIF
              GET
        ENDP

If you try to reorder a file which is already open (i.e. shown in bold on the System screen) you will see a `‘File’ is in use`’ error. You should close the file and then try again.