Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabetical sort of characters within a string.
#19
(04-25-2024, 11:13 AM)bplus Wrote:
(04-25-2024, 04:50 AM)CharlieJV Wrote:
Code: (Select All)
a$ = "this is uncopyrightable"
FOR i% = 1 TO 26
  FOR j% = 1 TO LEN(a$)
    t$ = UCASE$( MID$(a$,j%,1) )
    IF ASC(t$) = 64 + i% THEN PRINT t$;
  NEXT
NEXT

@CharlieJV almost same as Pete's only without instr both suffer from taking ucase$ or lcase$ more often than needed, asc can take a 2nd argument and work faster than mid$ for single letter.
Code: (Select All)
a$ = "this is uncopyrightable"
ua$ = UCase$(a$) ' do this once
For i% = 1 To 26
    For j% = 1 To Len(a$)
        If Asc(ua$, j%) = 64 + i% Then Print Chr$(i% + 64);
    Next
Next

i think instr will go faster than testing every letter in word 26 times.

i am suspecting steve's or my mod of anacode$ with string$ will be faster than pete's using instr in alpha order, i like the fact we count over the length of word only once and there is no if testing when producing the return string from counts array. it is count len(word$) + 26 string alpha steps versus instr length of word and if find instr again and another if find... length of word x 2 ifs over 26 letters steps ie they both have to go 26 letter steps but no if's in steve's/my mod with string$.

i will test by anacoding a dictionary with both methods, might consider throwing out the need for ucase$/lcase$ as entire dictionary is all capitals to start.

Yeah, I could have called UCASE$ once outside of the looping, but I was going for shortest amount of code vs performance.

That said, UCASE$ x times for a single character vs UCASE$ 1 time for x characters, would there be a difference? 

Yeah, not in love with that second parameter for ASC.  I'm a little bit too much old school there.
Reply


Messages In This Thread
RE: Alphabetical sort of characters within a string. - by CharlieJV - 04-25-2024, 12:17 PM



Users browsing this thread: 6 Guest(s)