Hello group, here’s my question for today I’m studying arrays … I need to sort A total for each month of the year … I managed to do it by creating two separate arrays, which use the same index, in the first one there I put the sums and in the second the months. When I order the first, the value of the second is automatically moved too. I used this method:
Var i,j as integer
var temp as currency
var temp2 as string
for i=0 to 11
for j=0 to 11
if SommeAndamentoNettoMensile(j)>SommeAndamentoNettoMensile(i) then
temp=SommeAndamentoNettoMensile(i)
temp2=Mese_SommeAndamentoNettoMensile(i)
SommeAndamentoNettoMensile(i)=SommeAndamentoNettoMensile(j)
Mese_SommeAndamentoNettoMensile(i)=Mese_SommeAndamentoNettoMensile(j)
SommeAndamentoNettoMensile(j)=temp
Mese_SommeAndamentoNettoMensile(j)=temp2
end if
next j
next i
For i=0 to 11
messagebox SommeAndamentoNettoMensile(i).ToText + " mese di " +Mese_SommeAndamentoNettoMensile(i)
next i
I was reading the xojo documentation, I saw the possibility of creating two-dimensional arrays and sorting the arrays with the sort or .SortWith method … My sorted data then I have to transfer to a listbox. My question is this: Is there a better method I can use since it’s not that much data anyway? Or, if I transfer them to a listbox can I sort the listbox faster?
In the end, I’m interested in having a listbox where I can read the sorted values: January 100
February 130
March 400 …etc…etc…
To answer the other question it will always be faster to sort the arrays and prior to loading them into the control. Don’t use visual items (controls etc that have a visual interface) to perform operations such as sorting. Unless that is you want the user to be able to alter your sorting methods themselves. For example you have two columns Month and Total for that month and want the user to be able to sort by either.
Ok, I understood and already used the sort method to sort an array… my problem is that I didn’t understand until recently, how can I sort two arrays simultaneously related to each other.
Thanks to Tim Hare, I studied the sortwith function a bit and solved my whole problem, eliminating many lines of code with:
Var j as integer
var temp as currency
var temp2 as string
for i=0 to 11
for j=0 to 11
if SommeAndamentoNettoMensile(j)>SommeAndamentoNettoMensile(i) then
temp=SommeAndamentoNettoMensile(i)
temp2=Mese_SommeAndamentoNettoMensile(i)
SommeAndamentoNettoMensile(i)=SommeAndamentoNettoMensile(j)
Mese_SommeAndamentoNettoMensile(i)=Mese_SommeAndamentoNettoMensile(j)
SommeAndamentoNettoMensile(j)=temp
Mese_SommeAndamentoNettoMensile(j)=temp2
end if
next j
next i
ListBoxAndamentoMese.RemoveAllRows
i=11
while i>=0
ListBoxAndamentoMese.AddRow(i.ToText,Mese_SommeAndamentoNettoMensile(i).ToText,format(double.FromString(SommeAndamentoNettoMensile(i).ToText),"#.00"))
i=i-1
wend
NEXT XOJO code:
SommeAndamentoNettoMensile.sortwith(Mese_SommeAndamentoNettoMensile)
while i>=0 ListBoxAndamentoMese.AddRow(i.ToText,Mese_SommeAndamentoNettoMensile(i).ToText,format(double.FromString(SommeAndamentoNettoMensile(i).ToText),"#.00"))
i=i-1
wend