# Say what, Solitaire?

April 20 2006 at 9:40 AM
Forum Owner

Response to Sorting by two variables

> The least significant field is sorted first, ...

Heh - reminds me of the IBM card sorter where indeed I sorted by least significant character of least significant field, then collected in order from the 10 slots and did it again and again until reaching the most significant. <whew!>

The on pass technique I showed here, however, sorts from most to least.

Here is a demo with three fields sorted.

Mac

CLS : PRINT "Building Test Data"
CONST Size = 200
DIM a(Size) AS INTEGER
DIM b(Size) AS INTEGER
DIM c(Size) AS INTEGER
FOR i = 1 TO Size
a(i) = 1001 + INT(RND * 5)
b(i) = 2001 + INT(RND * 20)
c(i) = i
NEXT i
GOSUB ShowWhatsThere
PRINT : PRINT : PRINT "Primary(a) Secondary(b) LeastSignificant(c)"
FOR i = 1 TO Size - 1
FOR j = i TO Size
IF a(i) > a(j) THEN
SWAP a(i), a(j)
SWAP b(i), b(j)
SWAP c(i), c(j)
ELSEIF a(i) = a(j) THEN
IF b(i) > b(j) THEN
SWAP a(i), a(j)
SWAP b(i), b(j)
SWAP c(i), c(j)
ELSEIF b(i) = b(j) THEN
IF c(i) > c(j) THEN
SWAP a(i), a(j)
SWAP b(i), b(j)
SWAP c(i), c(j)
END IF
END IF
END IF
NEXT j
NEXT i
GOSUB ShowWhatsThere
SYSTEM

ShowWhatsThere:
FOR i = 1 TO Size STEP 10
FOR j = 0 TO 9
PRINT a(i + j); b(i + j); c(i + j)
NEXT j
LOCATE , 1, 1
PRINT "Press any key except ESC to continue printing: ";
DO: k\$ = INKEY\$: LOOP WHILE k\$ = ""
LOCATE , 1, 1: PRINT SPACE\$(70);
LOCATE , 1, 1
IF k\$ = CHR\$(27) THEN RETURN
NEXT i
RETURN

 Respond to this message
Responses

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums