Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Alphabetically
#1
Old timer trying to recreat a sort routine I did back in the 80s to sort a list of items alphabetically according to the variable ID$. See code below that does not seem to work. Success seems to hinge on comparing value of the string variable.
Any Suggestions??
700 REM ALPHABETIZE DATA BY DESCRIPTION = ID$
        FOR N = 1 TO NOR
        FOR M = 1 TO NOR - 1
        IF ID$(M)> ID$(M + 1) THEN GOTO 720 ELSE GOTO 799
720    SCTEMP$ = SC$(M)
        IDTEMP$ = ID$(M)
        UCSTTEMP = UCST(M)
        TQTYTEMP = TQTY(M)
    REM SWAP LINE ITEMS IN LIST
        SC$(M) = SC$(M + 1)
        ID$(M) = ID$(M + 1)
        UCST(M) = UCST(M + 1)
        TQTY(M) = TQTY(M + 1)
        SC$(M + 1) = SCTEMP$
        ID$(M + 1) = IDTEMP$
        UCST(M + 1) = UCSTTEMP
        TQTY(M + 1) = TQTYTEMP
799    NEXT M
      NEXT N
        RETURN 502
Reply
#2
Well, I added some code to make your program function, but I don't have time right now to find the bug. Maybe later unless someone else finds it.

Run this and note that sort does not work.

Code:
CONST NOR = 5
DIM id$(NOR), sc$(NOR), UCST(NOR), TQTY(NOR)
FOR i = 1 TO NOR
  id$(i) = STR$(INT(RND * 1000))
  sc$(i) = "SC" + STR$(i)
  UCST$(i) = "UCST" + STR$(i)
  TQTY$(i) = "TQTY" + STR$(i)
NEXT i

CLS
FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
PRINT "--------"

REM ALPHABETIZE DATA BY DESCRIPTION = ID$
FOR N = 1 TO NOR
  FOR M = 1 TO NOR - 1
    IF id$(M) > id$(M + 1) THEN
      SCTEMP$ = sc$(M)
      IDTEMP$ = id$(M)
      UCSTTEMP = UCST(M)
      TQTYTEMP = TQTY(M)
      REM SWAP LINE ITEMS IN LIST
      sc$(M + 1) = SCTEMP$
      id$(M + 1) = IDTEMP$
      UCST(M + 1) = UCSTTEMP
      TQTY(M + 1) = TQTYTEMP
    END IF
  NEXT M
NEXT N

FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
Reply
#3
Somehow you lost these commands:
Code:
      REM Part 1 of the swap
      sc$(M) = sc$(M + 1)
      id$(M) = id$(M + 1)
      UCST(M) = UCST(M + 1)
      TQTY(M) = TQTY(M + 1)

Here they are inserted
Code:
CONST NOR = 5
DIM id$(NOR), sc$(NOR), UCST(NOR), TQTY(NOR)
FOR i = 1 TO NOR
  id$(i) = STR$(INT(RND * 1000))
  sc$(i) = "SC" + STR$(i)
  UCST$(i) = "UCST" + STR$(i)
  TQTY$(i) = "TQTY" + STR$(i)
NEXT i
CLS
FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
PRINT "--------"
REM ALPHABETIZE DATA BY DESCRIPTION = ID$
FOR N = 1 TO NOR
  FOR M = 1 TO NOR - 1
    IF id$(M) > id$(M + 1) THEN
      SCTEMP$ = sc$(M)
      IDTEMP$ = id$(M)
      UCSTTEMP = UCST(M)
      TQTYTEMP = TQTY(M)
      REM Part 1 of the swap
      sc$(M) = sc$(M + 1)
      id$(M) = id$(M + 1)
      UCST(M) = UCST(M + 1)
      TQTY(M) = TQTY(M + 1)
      REM Part 2 of the swap
      sc$(M + 1) = SCTEMP$
      id$(M + 1) = IDTEMP$
      UCST(M + 1) = UCSTTEMP
      TQTY(M + 1) = TQTYTEMP
    END IF
  NEXT M
NEXT N

FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
Reply
#4
Good going, Mac! Smile 
Only thing is, first two array variables are strings, but, second two are numerals, not strings Sad
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#5
(01-04-2008, 02:47 AM)Ralph link Wrote:Good going, Mac! Smile  Only thing is, first two array variables are strings, but, second two are numerals, not strings Sad

Good catch. I didn't bother to display those two, so didn't notice the bug.

I presume his main program has everything initialized properly.

Mac
Reply
#6
Mac,
Thanks for your efforts. As it turns out, the code I gave works perfectly now, i.e. I did not change anything in that list. I did make some other small changes in my program and, as you suggest, it may have been an initialization error.
Sorry for the confusion and thanks again for your responses.
John
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)