Control Sorting of an Array

Hello,

I have an array - my_array - that contains random numbers

Beyond sort is there a way to control the direction of sorting an Array such as ascending or descending like you can for a listbox?

Thanks

Sort it ascending and if you need it descending then reverse the array.

I have read the Language Reference and there is no reference to direction for sorting an array
https://documentation.xojo.com/index.php/Sort

Sort and SortWith will sort the array ascending. There is no option. You can access the array in descending order by reversing your loop.

// ascending
for n = 0 to Ubound(myArray)
// descending
for n = Ubound(myArray) DownTo 0

Good suggestion, I did not think of traversing the array like that.

This would work good if I had ascending - descending information contained within myArray however the array is randomly filled with numbers. I was looking for a way to sort like we can in a ListBox column and do this in a manner for numbers and text so a real ascending/descending sort exists when the user clicks on the ComboBox

while you might not be able to easily control the direction of the sort you can pretty generically traverse an array in the right direction to make it seem like you have sorted ascending & descending options

Something like

const sortDescending = false
dim myarray() as integer = Array(1,4,7,8,2,4,3,9,12)
dim start as integer
dim limit as integer
dim stepsize as integer
if sortDescending then
    start = ubound(myarray)
    limit = 0
    stepsize = -1
else
    start = 0
    limit = ubound(myarray)
    stepsize = 1
end if
  
myarray.sort
  
for i as integer = start to limit step stepsize
    listbox1.addrow str(myarray(i) )
next

Look up the array methods, Sort and SortWith, that I mentioned above.