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"))
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