Microsoft Small Basic

Program Listing: JLS539-1
' Text Compare
' Version 0.3
' Copyright © 2017-2019 Nonki Takahashi. The MIT License.
' Program ID JLS539-1

param = "text1=fire;text2=File;"
Text_Compare()
ShowResult()
param = "text1=Alpha;text2=beta;"
Text_Compare()
ShowResult()
param = "text1=Windows;text2=windows;"
Text_Compare()
ShowResult()
param = "text1=Windows;text2=windows;caseSensitive=True;"
Text_Compare()
ShowResult()
param = "text1=Small;text2=Small;"
Text_Compare()
ShowResult()

Sub ShowResult
If eq Then
TextWindow.Write(param["text1"] + " = " + param["text2"])
EndIf
If gt Then
TextWindow.Write(param["text1"] + " > " + param["text2"])
EndIf
If lt Then
TextWindow.Write(param["text1"] + " < " + param["text2"])
EndIf
If param["caseSensitive"] Then
TextWindow.WriteLine(" (case sensitive)")
Else
TextWindow.WriteLine("")
EndIf
EndSub

Sub Text_Compare
' Text | compare texts with case sensitive option
' param["text1"], param["text2"] - to compare
' param["caseSensitive"] - "True" if case sensitive
' return eq = "True" if text1 = text2
' return gt = "True" if text1 > text2
' return lt = "True" if text1 < text2
If param["caseSensitive"] Then
text1 = param["text1"]
text2 = param["text2"]
Else
text1 = Text.ConvertToLowerCase(param["text1"])
text2 = Text.ConvertToLowerCase(param["text2"])
EndIf
If (text1 + 0 = text1) And (text2 + 0 = text2) Then
' both text1 and text2 are numbers
eq = "False"
gt = "False"
lt = "False"
If text1 = text2 Then
eq = "True"
ElseIf text1 > text2 Then
gt = "True"
ElseIf text1 < text2 Then
lt = "True"
EndIf
Else
Text_CompareTexts()
EndIf
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