#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip
Sub Class_Globals
Type Person(Group As Int, Name As String, Address As String, City As String, Age As Int)
Private Root As B4XView 'ignore
Private xui As XUI
Private missing As Int = -1
End Sub
Public Sub Initialize
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Dim group(5) As List
For i = 0 To 4
group(i).Initialize 'now we can add individuals to each group list
Next
Dim names1() As String = Array As String("John", "Alice", "Anne", "Michael", "Gordon", "William") 'groups need not be the same size
Dim names2() As String = Array As String("Alphonse", "Mark", "Louise", "Pamela")
'add these persons to groups 0 and 1
For Each pname As String In names1
group(0).Add(CreatePerson(0, pname, "", "", missing))
Next
For Each pname As String In names2
group(1).Add(CreatePerson(1, pname, "", "", missing))
Next
'sort each group() by name
For i = 0 To 4
group(i).SortType("Name", True)
Next
'index all individuals by name
Dim ByName As Map
ByName.Initialize
For i = 0 To 4
For j = 0 To group(i).Size - 1
Dim indiv As Person = group(i).Get(j)
ByName.Put(indiv.Name, indiv) 'this points to the same object as the one in the group list
Next
Next
'Add age to a individual when it becomes available - note inline casting to Person
ByName.Get("Anne").As(Person).Age = 22 'this changes not only the item in the map but also the item in the group list - they are the same
'show group(0) individuals
Log($"Name${TAB}Age${TAB}Group"$)
Log(TAB)
For Each indiv As Person In group(0)
Log(indiv.Name & TAB & IIf(indiv.Age=missing, "-", indiv.age) & TAB & indiv.group)
Next
'Log results(note that they are sorted and age is updated)
'Name Age Group
'
'Alice - 0
'Anne 22 0
'Gordon - 0
'John - 0
'Michael - 0
'William - 0
End Sub
Public Sub CreatePerson (Group As Int, Name As String, Address As String, City As String, Age As Int) As Person
Dim t1 As Person
t1.Initialize
t1.Group = Group
t1.Name = Name
t1.Address = Address
t1.City = City
t1.Age = Age
Return t1
End Sub