Sort an array (step by step)

Hello group, here’s my question for today :slight_smile: 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…

You don’t need to use 2-dimensional arrays. A 1-dimensional array is quite enough.

I often use the SortWith method. In my app - just one example - I need to sort emails clients before IMAP accounts which is done with SortWith.

Ok so my example is fine with two one dimensional arrays… but if I use sort on the first one, how do I sort the second one based on the first one?

Use SortWith instead of Sort.

var a() as integer
var b() as string
a.sortwith(b)

You can add as many arrays as you want and they will all be sorted by a.

a.sortwith(b, c, d, e, f)

1 Like

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.

you can also use a comparer method to sort rows.
this comparer get two objects as input
and answer by -1,0,1 as < = >

.Sort(AddressOf methodName)
https://documentation.xojo.com/api/language/arrays.html#arrays-sort

Yes yes, in fact I order them and then I load them on a ListBox

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:

SommeAndamentoNettoMensile.sortwith(Mese_SommeAndamentoNettoMensile)

and filling the listbox with the sorted data.

Initial code that work fine:

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

TOP ! :slight_smile:

1 Like

Thanks TOM, sortwith that’s the function I needed :slight_smile: