dkl
Full Member
Posts: 234
|
Post by dkl on Oct 5, 2021 1:13:01 GMT -5
Could someone please explain what is wrong with the code below?
If I cut out the 'sort()' command from this code it will print out the array
If I add the 'sort()' command to this code it will NOT print out the array and I get an error If I Dim a$(12,12) then it will only print out " - "
If I change sort a$(), 12,12,1 then it will print the array, but it will NOT sort the array in order of names
I actually want to sort the array based on column 2 (the values). I realise that I should put '2' as the last digit in the sort command
Any help would be appreciated. Thank You
Dim a$(2,12)
a$(1,1) = "John": a$(2,1) = str$(3) a$(1,2) = "Jim": a$(2,2) = str$(10) a$(1,3) = "Mary": a$(2,3) = str$(5) a$(1,4) = "Josie": a$(2,4) = str$(3) a$(1,5) = "Fred": a$(2,5) = str$(21) a$(1,6) = "Betty": a$(2,6) = str$(2) a$(1,7) = "Garry": a$(2,7) = str$(17) a$(1,8) = "Robert": a$(2,8) =str$(9) a$(1,9) = "Freda": a$(2,9) = str$(3) a$(1,10) = "Amy": a$(2,10) = str$(2) a$(1,11) = "Ailsa": a$(2,11) = str$(15) a$(1,12) = "Leila": a$(2,12) = str$(8)
sort a$(), 1, 12, 1
for a = 1 to 12 print a$(1,a);" - ";a$(2,a) next a
|
|
|
Post by tsh73 on Oct 5, 2021 2:35:33 GMT -5
"You are doing it wrong" (sorry cannot resist ) first index supposed to be long one (row number, lot's of rows) second - column number If you see help on SORT you will see it. So basically you need to transpose array. Also SORT has a bug (I think) there it dies if number of rows is less thern number of columns. Dim a$(2,12)
a$(1,1) = "John": a$(2,1) = str$(3) a$(1,2) = "Jim": a$(2,2) = str$(10) a$(1,3) = "Mary": a$(2,3) = str$(5) a$(1,4) = "Josie": a$(2,4) = str$(3) a$(1,5) = "Fred": a$(2,5) = str$(21) a$(1,6) = "Betty": a$(2,6) = str$(2) a$(1,7) = "Garry": a$(2,7) = str$(17) a$(1,8) = "Robert": a$(2,8) =str$(9) a$(1,9) = "Freda": a$(2,9) = str$(3) a$(1,10) = "Amy": a$(2,10) = str$(2) a$(1,11) = "Ailsa": a$(2,11) = str$(15) a$(1,12) = "Leila": a$(2,12) = str$(8)
'sort a$(), 1, 12, 1
print "source array" for a = 1 to 12 print a$(1,a);" - ";a$(2,a) next a
dim b$(12,2)
for a = 1 to 12 b$(a,1)=a$(1,a) b$(a,2)=a$(2,a) next a
print "sort by name"
sort b$(), 1, 12, 1
for a = 1 to 12 print b$(a,1);" - ";b$(a,2) next a print "sort by str$(number)"
sort b$(), 1, 12, 2
for a = 1 to 12 print b$(a,1);" - ";b$(a,2) next a
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Oct 5, 2021 2:49:35 GMT -5
Ahh.....Thank you for that, tsh73. I have used two dimensioned array$ before the same way with out a problem but never used sort() with it However, I will make note of your comments and do it your way in future. I would like to be able to change sort by number to sort by value so I get .....21,17,15,10,9,8 etc rather than 10,15,17,2,2,21,3,5,7 etc
Is that possible?
|
|
|
Post by tsh73 on Oct 5, 2021 3:59:39 GMT -5
By using extra column to sorn on
Dim a$(2,12)
a$(1,1) = "John": a$(2,1) = str$(3) a$(1,2) = "Jim": a$(2,2) = str$(10) a$(1,3) = "Mary": a$(2,3) = str$(5) a$(1,4) = "Josie": a$(2,4) = str$(3) a$(1,5) = "Fred": a$(2,5) = str$(21) a$(1,6) = "Betty": a$(2,6) = str$(2) a$(1,7) = "Garry": a$(2,7) = str$(17) a$(1,8) = "Robert": a$(2,8) =str$(9) a$(1,9) = "Freda": a$(2,9) = str$(3) a$(1,10) = "Amy": a$(2,10) = str$(2) a$(1,11) = "Ailsa": a$(2,11) = str$(15) a$(1,12) = "Leila": a$(2,12) = str$(8)
'sort a$(), 1, 12, 1
'print "source array" 'for a = 1 to 12 'print a$(1,a);" - ";a$(2,a) 'next a
'dim b$(12,2) dim b$(12,3)
for a = 1 to 12 b$(a,1)=a$(1,a) b$(a,2)=a$(2,a) b$(a,3)=right$("00000"+b$(a,2),5) '10->00010, 7->00007. So 10 sorts above 7 next a
print "sort by 00000str$(number), descending"
'sort b$(), 1, 12, 3 sort b$(), 12, 1, 3 'descending sort
for a = 1 to 12 print b$(a,1),b$(a,2),b$(a,3) next a
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Oct 5, 2021 5:06:22 GMT -5
That is really neat! Thank you
|
|