Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Array sort using an ANSI collate string for all characters

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array sort using an ANSI collate string for all characters

    ' This program bit illustrates array sorting using a user
    ' defined collate string. The sorting sequence used by
    ' Windows e.g. in listboxes may not be adequate for all
    ' purposes even after adjusting for the specific language
    ' using the LOCALE-functions provided by Windows. In this
    ' program you can specify your own sorting order in a
    ' so-called COLLATE STRING to be used by ARRAY SORT.
    '
    ' The COLLATE STRING devised in the program is based on the ANSI
    ' character set. So if you use the OEM character set, the
    ' string will be useless. The examples in the PowerBasic help
    ' file are based on the OEM character set. They are not applicable
    ' for the ANSI character set.

    ' This version produces a collate string which gives all versions
    ' (including upper and lower case) of a given character the same
    ' sorting "weight".

    ' The Scandinavian characters and other special European characters
    ' are included.

    ' Slight modification could be necessary for some European
    ' languages. If so, I would be happy to know the specific changes
    ' needed. All comments and suggestions are welcome.

    ' Erik Christensen, Copenhagen, Denmark. e.chr@email.dk
    ' -----------------------------------------------------------------
    Code:
    #COMPILE EXE
    #DIM ALL
    #REGISTER NONE
    #INCLUDE "WIN32API.INC"
    
    %ID_LIST1 = 101
    
    DECLARE CALLBACK FUNCTION DlgProc
    
    GLOBAL CollateString AS STRING
       ' Collate string for sorting string arrays.
       ' This should be global and initialized
       ' only once at the start of the program
    
    FUNCTION PBMAIN
        LOCAL hDlg AS LONG
        LOCAL I AS INTEGER
        LOCAL J AS INTEGER
        LOCAL Y AS LONG
        LOCAL font AS LONG
        DIM SortArray(1000) AS GLOBAL STRING
        DIALOG NEW 0,"String Array Sorting",,, 100, 252, %WS_CAPTION OR %WS_SYSMENU TO hdlg
        CONTROL ADD LISTBOX,hDlg,%ID_LIST1,SortArray(),5,5,90,245, _
            %WS_VSCROLL OR %WS_HSCROLL OR %WS_TABSTOP,%WS_EX_CLIENTEDGE
        Font=GetStockObject(%SYSTEM_FIXED_FONT)
        CONTROL SEND hDlg,%ID_LIST1,%WM_SETFONT,Font, %TRUE
    
        CollateString=""
        CALL MakeCharacterCollateString(CollateString)
    
        ' make random string array
        FOR I=0 TO 1000
            SortArray(I)=""
            FOR J=1 TO 30
                Y = RND(65, 255)
                SELECT CASE Y
    
                    'All characters filter
                     CASE 65 TO 90,97 TO 122,192 TO 214, 216 TO 221, 224 TO 246, 248 TO 253,255
    
                    'English character set filter
                    'CASE 65 TO 90,97 TO 122
    
                    'Scandinavian character set filter
                    'CASE 65 TO 90,97 TO 122, 196 TO 198,214,216,228 TO 230,246,248
    
                        SortArray(I)=SortArray(I)+CHR$(Y)
    
                END SELECT
            NEXT
        NEXT
        ' sort array according to the collate string
        ARRAY SORT SortArray(), COLLATE CollateString, ASCEND
    
        ' display sorted array in the listbox
        FOR I=0 TO 1000
          LISTBOX ADD hDlg,%ID_LIST1,SortArray(I)
        NEXT
    
        DIALOG SHOW MODAL hdlg CALL DlgProc
    END FUNCTION
    
    CALLBACK FUNCTION DlgProc
        ' not used
    END FUNCTION
    
    SUB MakeCharacterCollateString(BYREF CS AS STRING)
        LOCAL I AS INTEGER, _
              K AS INTEGER
        ' start with the normal sequence of characters. In this starting
        ' string each character takes its normal or usual position.
        FOR I = 0 TO 255
            CS = CS + CHR$(I)
        NEXT
    
        ' Put upper case letter codes at position of lower case letters.
        ' This will give the lower case letters the same sorting "weight"
        ' as the upper case letters. Each character will be sorted
        ' according to the ASCII character value placed at its normal
        ' (usual) position in the collate string. This ASCII character
        ' value defines the sorting "weight" of the character normally
        ' occupying this position.
        MID$(CS, 97+1, 26) = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    
        ' Put upper case letter codes at position of corresponding
        ' special characters (upper or lower case). This will give
        ' these special characters the same sorting "weight" as their
        ' "mother" upper case letter. Put [\] at position of æäøöå
        ' and ÆÄØÖÅ to ensure correct sorting of these Scandinavian
        ' characters.
        FOR K=0 TO 32 STEP 32
            MID$(CS, 192+1+K, 23) = "AAAA[][CEEEEIIIIDNOOOO\"
            MID$(CS, 216+1+K, 6)  = "\UUUYY"
        NEXT
        MID$(CS, 255+1, 1)  = "Y"
        MID$(CS, 159+1, 1)  = "Y"
        MID$(CS, 138+1, 1)  = "S"
        MID$(CS, 154+1, 1)  = "S"
        MID$(CS, 140+1, 1)  = "["
        MID$(CS, 156+1, 1)  = "["
    
    END SUB


    ------------------




    [This message has been edited by Erik Christensen (edited January 31, 2001).]
Working...
X