Android Question Order String

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
please I need some help ordering a string.

This string: C1, P5, S3, C5, S9, C3, S1, P1
To this: P1, P3, S3, S9, C1, C3, C5

So how to order? The strings with letter "P" must be the first, then "S" and "C" finally
 

sorex

Expert
Licensed User
Longtime User
indeed you explain it there in a simular way, the only thing you accidently forgot was the prefix order.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
actually if you play it smart you don't need the type when you concatenate it all to 1 object

B4X:
Dim data As String = "C1, P5, S939, S3, C5, S9, C3, S1, P1, S10"
Dim output,items(),prefix,num As String
Dim l As List
l.Initialize
items=Regex.Split(", ",data)
For x=0 To items.Length-1
num=("000000"&items(x).SubString(1))
If items(x).StartsWith("P") Then prefix="1"
If items(x).StartsWith("S") Then prefix="2"
If items(x).StartsWith("C") Then prefix="3"
l.Add(prefix &"-"&  num.SubString(num.Length-5) & "-" & items(x))
Next
l.Sort(True)
For x=0 To l.Size-1
output=output & Regex.Split("-",l.Get(x))(2)
If x<l.Size-1 Then output=output&", "
Next
Log(output)

:)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
@imgsimonebiliato : this one will be the easiest for you to handle. just extend the prefix lines when there are new items.

B4X:
Dim data As String = "C1, P5, S939, S3, C5, S9, C3, S1, P1, S10"
Dim output,items(),prefix,num As String
Dim l,p As List
p.Initialize
p.AddAll(Array As String("P","S","C")) ' <- here
l.Initialize
items=Regex.Split(", ",data)
For x=0 To items.Length-1
num=("000000"&items(x).SubString(1))
l.Add(p.IndexOf(items(x).SubString2(0,1)) &"-"&  num.SubString(num.Length-5) & "-" & items(x))
Next
l.Sort(True)
For x=0 To l.Size-1
output=output & Regex.Split("-",l.Get(x))(2)
If x<l.Size-1 Then output=output&", "
Next
Log(output)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
@wonder:
Your function returns for this string: C1, P50, S3, C5, S10, C3, S1, P1
a new string like this: €P1, €P50, S1, S10, S3, ‚C1, ‚C3, ‚C5
But I think the old boy wants S3 to be ahead of S10.

Here's the new version:
B4X:
Sub SetCustomOrder(originalString As String, customOrder As String, ascending As Boolean) As String
    Dim tempString, finalString   As String
    Dim tempList(2)               As List
    For co = 0 To (customOrder.Length - 1)
        tempList(0).Initialize
        tempList(1).Initialize
        For os = 0 To (originalString.Length - 1)
            If originalString.CharAt(os) = customOrder.CharAt(co) Then
                tempList(0).Add(originalString.CharAt(os))
                For n = Min(os + 1, (originalString.Length - 1)) To (originalString.Length - 1)
                    If Asc(originalString.CharAt(n)) >= Asc("0") And Asc(originalString.CharAt(n)) <= Asc("9") Then
                        tempString = tempString & originalString.CharAt(n)
                    Else
                        Exit
                    End If
                Next
                Dim num = tempString As Long
                tempList(1).Add(num)
                tempString = ""
            End If
        Next
        tempList(1).Sort(ascending)
        For l = 0 To (tempList(0).Size - 1)
            finalString = finalString & tempList(0).Get(l) & tempList(1).Get(l) & ", "
        Next
    Next
    Return finalString.SubString2(0, Max(0, finalString.Length - 2))
End Sub

Untitled.png
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I just wanted to apply the above approach to a SQLite table. This is not any better than wonder, sorex, udg or roycefer code. It is simply to illustrate how it can be done should someone encounter a similar situation while dealing with a SQLite table specific sorting.
1. I created a table with one column: ITEM TEXT and filled it with the following 10 rows of data stored: "C1","P5","S3","C5","S9","C3","S1","P1","S10","S939"
2. I wanted to sort with P, then S, then C and the numerical part of each data item.
Here is the code to do it:
B4X:
txt="SELECT ITEM, CASE WHEN substr(ITEM,1,1)= 'P' THEN 1 WHEN substr(ITEM,1,1)= 'S' " _ 
    & "Then 2 Else 3  End As RANK FROM tblOriginal ORDER BY RANK, CAST(substr(ITEM,2) As INTEGER)"
    Cursor1=SQL1.ExecQuery(txt)
After execution of the query, the display order becomes: P1,P5,S1,S3,S9,S10,S939,C1,C3,C5
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
nice one, @Mahares. I used simular tricks a decade ago in ASP as fall back when I had to sort multi dimensional arrays and there was no real support for it.

although it requires an additional library and code it is or can be a lot easier to deal with than doing it the regular ways.
 
Upvote 0
Top