B4J Question Sorting PC's IP

XbNnX_507

Active Member
Licensed User
Longtime User
Hi, I'm new around here.
I'would like to know if changes can be made to the code below that i wrote, to be more efficient.
I don't know much about programming.

Given a quantity of pc's ip of the same subred ¡sort!.

problems arise if i tried to mix subred.

sample:
sort_ip(Array as string("192.168.1.1","192.168.12.1","192.168.9.1"))

B4X:
sort_ip( Array As String("192.168.1.32","192.168.1.20","192.168.1.1","192.168.1.3","192.168.1.2","192.168.1.22","192.168.1.222","192.168.1.25","192.168.1.11","192.168.1.9","192.168.1.6" ) )

Sub sort_ip( ips As List) As List
    Log("-> List of ip's " & ips)
    Dim numbers As List : numbers.Initialize
    Dim temp As List : temp.Initialize
    Dim sortedIp As List : sortedIp.Initialize
   
    temp.AddAll( ips)
   
    For i = 0 To temp.Size -1
        Dim last_digits,currCadena As String
        Dim num As Int
        currCadena = temp.Get(i)
        last_digits = currCadena.SubString( currCadena.LastIndexOf("."))
        num = last_digits.Replace(".","")
        numbers.Add( num)
    Next

    numbers.Sort(True)
   
    For x = 0 To numbers.Size-1
        Dim s As String 
        s = numbers.Get(x)
        If s.StartsWith("") Then
            numbers.Set(x,$".${s}"$)
            For i = 0 To temp.Size-1
                Dim s1 As String
                s1 = temp.Get(i)
                If s1.EndsWith( numbers.Get(x) ) Then
                    sortedIp.Add( s1 )
                    Exit
                End If
            Next
           
        End If
    Next
    'Log(numeros)

    Log($"Sorted -> ${sortedIp}"$)
    Return sortedIp
End Sub
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I believe that yes! Something like this:

B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Type IpAddress(First As Int, Second As Int, Third As Int, Fourth As Int)
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.SetFormStyle("UNIFIED")
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Show

Dim ipString As List = Array As String("192.168.1.32","192.168.1.20","192.168.1.1","192.168.1.3","192.168.1.2","192.168.1.22","192.168.1.222","192.168.1.25","192.168.1.11","192.168.1.9","192.168.1.6")

Dim IpAddressType As List
IpAddressType.Initialize
For i = 0 To ipString.Size - 1
Dim ipS As String = ipString.Get(i)
Dim ipAdd As IpAddress
ipAdd.Initialize

ipAdd.First = ipS.SubString2(0,3)
ipAdd.Second = ipS.SubString2(4,7)
ipAdd.Third = ipS.SubString2(8,ipS.IndexOf2(".",8) + 1)
If ipS.lastIndexOf(".") + 1 = ipS.Length - 1 Then
ipAdd.fourth = ipS.SubString(ipS.Length - 1)

Else
ipAdd.fourth = ipS.SubString2(ipS.lastIndexOf(".") + 1,ipS.Length)
End If

IpAddressType.Add(ipAdd)
Next

IpAddressType.SortType("Fourth",True)
For i = 0 To IpAddressType.Size - 1
Dim ipAdd As IpAddress = IpAddressType.Get(i)
Log(ipAdd.First&"."&ipAdd.Second&"."&ipAdd.Third&"."&ipAdd.fourth)
Next
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My implementation:

B4X:
Sub Process_Globals
   Type IpAndValue (ip As String, value As Long)
End Sub

Sub MySort(ips As List)
   Dim ip2 As List
   ip2.Initialize
   For Each ip As String In ips
     Dim iv As IpAndValue
     iv.ip = ip
     For Each s As String In Regex.Split("\.", ip)
       iv.value = 256 * iv.value + s
     Next
     ip2.Add(iv)
   Next
   ip2.SortType("value", True)
   Dim sortedIps As List
   sortedIps.Initialize
   For Each iv As IpAndValue In ip2
     sortedIps.Add(iv.ip)
   Next
   Log(sortedIps)
End Sub
 
Upvote 0

XbNnX_507

Active Member
Licensed User
Longtime User
thanks Enrique Gonzales and Erel they both works great.
Erel's implementation is excellent. i wished to become like him one day.

i'll be posting threads regularly as i'm new in b4a, b4j and programming in general.
Greetings Erel.
 
Upvote 0
Top