B4J Question Sorting list where items start with an integer

kostefar

Active Member
Licensed User
Longtime User
Dear All,

The result of:

B4X:
Dim l As List
l.Initialize

l.Add("77 a")
l.Add("88 a")
l.Add("880 a")
l.Add("777 a")
l.Sort(True)
For i = 0 To l.Size - 1
  
    Log (l.Get(i))
Next

.. is

B4X:
77 a
777 a
88 a
880 a

and I want it to be

B4X:
77 a
88 a
777 a
888 a

It works if I don´t add the numbers as text, but in this case I need to have some text showing in each list item after the number.
Any hints?

Thanks in advance!
 
Last edited:

kostefar

Active Member
Licensed User
Longtime User
Add zeroes ("0") on the left, to have all items with the same lenght.

When you must show them, you can remove the useless zeroes.


[I remember that many "0" must be write "zeroes"; is this correct? :p]
I think I get you, so padding 0´s .. if largest number expected is 1000, then you´d add 3 zeros to anything below 10, 2 to anything below 100 etc?

EDIT: Yup, did the trick:

B4X:
If pct < 10 Then pctstring = "000" & pct
                    If pct > 10 And pct < 100 Then pctstring = "00" & pct
                    If pct > 100 And pct < 1000 Then pctstring = "0" & pct
            ranklist.Add (pctstring & "   " & coin)

And afterwards

B4X:
Dim rankitem As String = ranklist.get(i)
    If rankitem.StartsWith("000") Then rankitem = rankitem.SubString(3)
            If rankitem.StartsWith("00") Then rankitem = rankitem.SubString(2)
            If rankitem.StartsWith("0") Then rankitem = rankitem.SubString(1)
            Log ("# " & i & " " & rankitem)
 
Last edited:
Upvote 0
Top