Android Question How to convert this vb.net Function?

Dimmis

New Member
Hello. I'm new in B4A programming. Only 2 days.
I Know a little vb.net programming and now i try to learn B4A.
I have an application (scrabble word finder) in vb.net.
Until now I have convert some code from vb.net to B4A.

I have problem with this Function.

B4X:
Public Function SortChars(ByVal sumString As String) As String
        Dim sortme = sumString.ToList()
        sortme.Sort(Function(ca, cb) ca.CompareTo(cb))
        Return New String(sortme.ToArray())
    End Function

This Function get a word from dictionary text file (as string) and convert it to characters array.

Example: If the word is "HELLO" then set characters to an array by alphabetical order = "EHLLO"
another: word "ANYWHERE" return = AEEHNRWY

Any solution please?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a bit longer in this case as we don't have an internal method to convert a string to list and vice versa:
B4X:
Sub SortChars(s As String) As String
   Dim l1 As List
   l1.Initialize
   For i = 0 To s.Length - 1
     l1.Add(s.CharAt(i))
   Next
   l1.Sort(True)
   Dim sb As StringBuilder
   sb.Initialize
   For Each c As Char In l1
     sb.Append(c)
   Next
   Return sb.ToString
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
Sub shuffle(t As String) As String
Dim r As Int
Dim chars() As String
chars=Regex.Split("",t)
For x=0 To chars.Length-1
    r=Rnd(0,chars.Length)
    t=chars(r)
    chars(r)=chars(x)
    chars(x)=t
Next
t=""
For x=0 To chars.Length-1
t=t & chars(x)
Next
Return(t)
End Sub

B4A doesn't seem to have an array to string join so that's the reason for the extra loop at the end.

Edit : ignore this post, I thought it just shuffled but now I see it's sorting the chars.
 
Upvote 0

Dimmis

New Member
Thanks for reply.
I will try the solutions.

Edit: Erel Solution is what i need. Thanks

THREAD SOLVED
 
Last edited:
Upvote 0
Top