Difficulty assigning elements to an array of custom type in BASIC

I have an array of custom types that I want to sort.
I tried different sort routines that did not work and traced it down to the swap section of the sort.

Swapping items in the array does not work:

REM  *****  BASIC  *****

public type aType
	num as integer
	str as string
end type

public arr(3) as aType	
public tmp as aType


Sub Main

	arr(0).str = "aaa"
	arr(0).num = 0
	arr(1).str = "bbb"
	arr(1).num = 1
	arr(2).str = "ccc"
	arr(2).num = 2
	arr(3).str = "ddd"
	arr(3).num = 3
	
	tmp = arr(2)
	arr(2) = arr(3)
	arr(3) = tmp
	
	dim inx as integer
	dim str as string
	str = ""
	for inx = 0 to ubound(arr)
		str = str & "arr(" & inx  & " ).num = " & arr(inx).num & chr$(13)
	next inx
	msgbox str

End Sub

PLEASE tell me what I am doing wrong.

If there is a built-in sort routine that can be used, please let me know.

INFO:
Version: 7.5.3.2 (X86_64) / LibreOffice Community
Build ID: 9f56dff12ba03b9acd7730a5a481eea045e468f3
CPU threads: 12; OS: Windows 10.0 Build 22621; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL threaded

I think we are dealing with an tdf#146802 bug by Andrew Pitonyak.

1 Like

quoting yourself as:

Experienced programmer (40+ years)

you should be able to use advanced programming-languages should’nt you?

from attrs import define

@define
class My_Class():
	num: int
	name: str

def main():
    example = [My_Class(5, "aaa"), My_Class(2, "fff"), My_Class(7, "bbb")]
    return sorted(example, key=lambda x: x.name)	

print(main())
#prints:
[My_Class(num=5, name='aaa'), My_Class(num=7, name='bbb'), My_Class(num=2, name='fff')]

Anyway, this code does it correctly:

REM  *****  BASIC  *****

public type aType
	num as integer
	str as string
end type

public arr(3) as Variant	
public tmp as Variant


Sub Main

	arr(0) = New aType
	arr(0).str = "aaa"
	arr(0).num = 0
	arr(1) = New aType
	arr(1).str = "bbb"
	arr(1).num = 1
	arr(2) = New aType
	arr(2).str = "ccc"
	arr(2).num = 2
	arr(3) = New aType
	arr(3).str = "ddd"
	arr(3).num = 3
	
	tmp = arr(2)
	arr(2) = arr(3)
	arr(3) = tmp
	
	dim inx as integer
	dim str as string
	str = ""
	for inx = 0 to ubound(arr)
		str = str & "arr(" & inx  & " ).num = " & arr(inx).num & ", arr(" & inx  & " ).str = " & arr(inx).str & chr$(13)
	next inx
	str = str & "tmp.num = " & tmp.num & ", tmp.str = " & tmp.str
	msgbox str

End Sub

…
I had understood the initial question to mean that it was actually about sorting (according to attributes).

Indeed, but, as sokol92 wrote, there is also a bug concerning some behaviors of arrays containing objects. Including in the code proposed by the OP.
The code I propose is a workaround for that bug, no more, no less :wink:.

Sorting is another story, and there is no built-in function for doing that in Basic.

And so did I.

Yet, it’s not clear what the OP wants to achieve. Second, as his example array is sorted already for each member in the custom type, things have to be detailed.

A first thought, though: there’s no such predefined function to sort BASIC arrays. Moreover to sort from custom types…
As one may find plenty of Sort() functions examples for VBScript on the web for simple variables, it might be interesting to try a (convoluted) way to deal with custom types: use a Collection as an temporary data container.

I didn’t test but the OP could try that process idea:

  1. Copy the (unsorted) array of custom type to a Collection, where the key would be the member to be sorted.
    Ex: oCollection.Add(Item, Item.str) 'Item is a var of custom type; here sorting would be on the str member.
  2. Create an array or simple variables to hold only the members of the custom type to be sorted (str in the example). Let’s name it MySortedArray.
  3. Sort that array
  4. Recreate the sorted array by reading the Collection items in the order of the newly sorted array
  5. NewArray(i) = oCollection(MySortedArray(i))

Though I didn’t test it, I think this would do the trick.
Of course, this is just a workaround, and I would not use it for big item numbers.

Otherwise, adjust a standard sorting algorithm to use the custom type member to be sorted on.

HTH,

1 Like

Fortunately, we don’t have to rely on BASIC these days!

My original post noted that I had difficulty sorting the array and traced the difficulty down to the most simple element: swapping the elements of an array.
The bug that sokol92 referenced explains the trouble I was having.
I ultimately used a different workaround by putting the pertinent data into an array of strings and sorting that using a quicksort algorithm.

I am marking this as the solution because it is the best answer.
While others have been helpful in suggesting ways to circumvent the problem, the real solution is to fix the internal handling of data arrays in the BASIC compiler.