Microsoft Small Basic

Program Listing: CSH007-0
' Text Sort
' Version 0.2
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Program ID CSH007-0

txt[1] = "YouTube"
txt[2] = "Programming"
txt[3] = "You"
txt[4] = "Professional"
txt[5] = "Life"
Text_Sort()
ShowResult()

Sub ShowResult
i = 0
While list[i] <> 0
i = list[i]
TextWindow.WriteLine(txt[i])
EndWhile
EndSub

Sub Text_Sort
' Text | sort txt array
' param txt[] - text array
' return list[] - sorted index list
n = Array.GetItemCount(txt)
list[0] = 1 ' start
list[1] = 0 ' end
For j = 2 To n
i = 0
text1 = txt[list[i]]
text2 = txt[j]
Text_CompareTexts()
While (list[i] <> 0) And lt
i = list[i]
text1 = txt[list[i]]
Text_CompareTexts()
EndWhile
' insert j after i
list[j] = list[i]
list[i] = j
EndFor
EndSub

Sub Text_CompareTexts
' Text | compare texts
' param text1, text2 - to compare
' return eq = "True" if text1 = text2
' return gt = "True" if text1 > text2
' return lt = "True" if text1 < text2
eq = "False"
gt = "False"
lt = "False"
txt1 = Text.ConvertToLowerCase(text1)
txt2 = Text.ConvertToLowerCase(text2)
len1 = Text.GetLength(txt1)
len2 = Text.GetLength(txt2)
len = Math.Min(len1, len2)
For p = 1 To len
c1 = Text.GetCharacterCode(Text.GetSubText(txt1, p, 1))
c2 = Text.GetCharacterCode(Text.GetSubText(txt2, p, 1))
If c1 > c2 Then
gt = "True"
p = len ' exit For
ElseIf c1 < c2 Then
lt = "True"
p = len ' exit For
Else
' compare case
c1 = Text.GetCharacterCode(Text.GetSubText(text1, p, 1))
c2 = Text.GetCharacterCode(Text.GetSubText(text2, p, 1))
If c1 > c2 Then
gt = "True"
p = len ' exit For
ElseIf c1 < c2 Then
lt = "True"
p = len ' exit For
EndIf
EndIf
EndFor
If gt = "False" And lt = "False" Then
If len1 > len2 Then
gt = "True"
ElseIf len1 < len2 Then
lt = "True"
Else
eq = "True"
EndIf
EndIf
EndSub