Welcome Guest, you are in: Login

Fruit Of The Shed

Navigation (MMBasic)






Search the wiki

»


'
' Sorting test of strings
'
' Problem: How to sort strings, on a Number inside the string
'
' 2017.08.25 Peter Svard
'
Cls
'
Dim m$(10) As string
m$(1)= "Knr: 151, Tom, No: 50, blablabla"
m$(2)= "Knr: 292, Phil, No: 75, blablabla"
m$(3)= "Knr: 313, Anna, No: 80, blablabla"
m$(4)= "Knr: 424, Harry, No: 56, blablabla"
m$(5)= "Knr: 155, Lisa, No: 37, blablabla"
m$(6)= "Knr: 235, Carl, No: 49, blablabla"
m$(7)= "Knr: 372, Greg, No: 51, blablabla"
m$(8)= "Knr: 168, Will, No: 21, blablabla"
m$(9)= "Knr: 189, Roy, No: 13, blablabla"
'
' Test to sorting string's on No: number
'
' using Bubble (or ripple) sort... algorithm
'
Dim temp$ As string = ""
arraysize = 9
For i = 1 To arraysize - 1
For j = 1 To arraysize - 1
' take out No: number from strings
p = Instr(1,m$(j),"No:") + 3 ' where is No:
no1 = Val(Mid$(m$(j),p,Len(m$(j))-p))
p = Instr(1,m$(j+1),"No:") + 3
no2 = Val(Mid$(m$(j+1),p,Len(m$(j+1))-p))
If no1 > no2 Then
temp$ = m$(j)
m$(j) = m$(j+1)
m$(j+1) = temp$
EndIf
Next j
Next i
'
' Print out the sorted results...
'
Print "- Result -"
For i = 1 To arraysize
Print m$(i)
Next i

End