        MAIN "",134

SORT    LD HL,NEWCAT
        LD DE,NEWCAT+1
        LD BC,#FFFF-NEWCAT
        XOR A
        LD (HL),A
        LDIR 
        LD HL,(TAB_SORT)
        PUSH HL
        LD A,(HL)
        LD BC,SOAD
        CALL TAB_HL
        JP (HL)

SORT_SEC
        POP HL
        INC HL
        LD DE,NEWCAT

SLP2    LD A,(HL)
        CP #FF
        RET Z
        LD (EXXT),A
        PUSH HL
        LD IX,CAT
        CALL SLP1
        POP HL
        INC HL
        JR SLP2

SLP1    LD A,(IX+0)
        AND A
        RET Z
        CP 1
        JR Z,SPP1
        LD A,(EXXT)
        CP (IX+13)
        CALL Z,OK_SORT
SPP1    LD BC,#10
        ADD IX,BC
        JR SLP1

OK_SORT PUSH IX
        POP HL
        LD BC,#10
        LDIR 
        RET 

; Сортировка по 3-х байтовому расширению
SORT_EXT
        POP HL
        INC HL
        LD DE,NEWCAT

SLP3    LD A,(HL)
        INC A
;       CP #FF
        RET Z
        PUSH DE
        LD DE,EXTS
        LD BC,3
        LDIR 
        POP DE
        PUSH HL
        LD IX,CAT
        CALL SLP4
        POP HL
        JR SLP3

SLP4    LD A,(IX+0)
        AND A
        RET Z
        CP 1
        JR Z,NOSLP

        LD A,(EXTS+0)
        CP "."
        JR Z,PS1
        CP (IX+8)
        JR NZ,NOSLP

PS1     LD A,(EXTS+1)
        CP "."
        JR Z,PS2
        CP (IX+9)
        JR NZ,NOSLP

PS2     LD A,(EXTS+2)
        CP "."
        JR Z,PS3
        CP (IX+10)
        JR NZ,NOSLP

PS3     CALL Z,OK_SORT
NOSLP   LD BC,#10
        ADD IX,BC
        JR SLP4

EXXT    DB 0
EXTS    DS 3
SORT_NAME DS 11

SOAD    DW SORT_SEC
        DW SORT_EXT
        DW SORT_ALL

;TABIY1  DB #00,#1B,#01,#FF
;TABIY3  DB #01,"C..","H..",#FF

; Сортировка по полному имени
SORT_ALL
        POP HL
        INC HL
        LD DE,NEWCAT

SLP5    LD A,(HL)
;       CP #FF
        INC A
        RET Z
        PUSH DE
        LD DE,SORT_NAME
        LD BC,11
        LDIR 
        POP DE
        PUSH HL
        LD IX,CAT
        CALL SLP6
        POP HL
        JR SLP5

SLP6    LD A,(IX+0)
        AND A
        RET Z
        CP 1
        JR Z,FU1

        PUSH IX
        CALL SRCHF
        POP IX
        JR NZ,FU1
        CALL OK_SORT
FU1     LD BC,#10
        ADD IX,BC
        JR SLP6

SRCHF   LD HL,SORT_NAME
        LD B,11
SLPDJ   LD A,(HL)
        CP "."
        JR Z,PS4
        CP (IX+0)
        RET NZ
PS4     INC HL
        INC IX
        DJNZ SLPDJ
        XOR A
        RET 

