ADDRESSOF and SORTARRAY questions

Previous topic - Next topic

CatouSan

Not sure if this topic is in the correct location.

Anyway, I noticed that manual states the "cmd%" argument of SORTARRAY  requires you to get the address of your own comparison function using ADDRESSOF or leave it as 0 to use the default behavior. The SORTARRAY function also allows you to input an array of any type into it.

My question's are.. Can I recreate SORTARRY's behavior with my own functions in glbasic? and if so How would I go about it?

MrTAToad

Yes, you can do and would be quite easy...

A simple example would be :

Code (glbasic) Select
count = BOUNDS(ScreenMode[], 0)
  WHILE notsorted
    notsorted = FALSE
    FOR i = 0 TO count - 2
      IF ScreenMode[i].width > ScreenMode[i+1].width AND _
         ScreenMode[i].height > ScreenMode[i+1].height
        swapmode = ScreenMode[i]
        ScreenMode[i] = ScreenMode[i+1]
        ScreenMode[i+1] = swapmode
        notsorted = TRUE
      ENDIF
    NEXT
  WEND

CatouSan

I know I can sort things lol, what I mean is that I want to be able to pass an array of any type into a function.

Hatonastick

#3
Thought it was done using BYREF?  Actually reading BYREF it says it isn't needed with arrays.  So it's just giving the array as reference.  eg.

Code (glbasic) Select

TYPE MyType
number
ENDTYPE

GLOBAL TypeArray[] AS MyType
DIM TypeArray[10]

// Assign each TypeArray[].number entry in TypeArray[] a random number.
MyFunction(TypeArray[])

FUNCTION MyFunction: myarray[]
  FOR i = 0 to LEN(myarray[] - 1)
    myarray[i] = RND(100)
  NEXT
ENDFUNCTION


I'd have thought that's all you do.

PS I'm rusty and this hasn't been tested so I'm probably wrong somewhere. :)
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

CatouSan

Sorry I'm being too vague about what I'm looking to do. Ok so the reason I brought up the built in sorting function is because it can sort ANY kind of array. I know how to pass an array into a function just fine, the only problem is I can't call the function and input an array of a different type than what I specified. Is there a certain specification I have to make to an argument to allow any kind of array I want to be passed into the same function (in the same argument) when called? I know it sounds weird, but I need to be able to do this for a project I am working on.




Hatonastick

Erm... Kitty?  Mr TA Toad?  This is _all_ yours!  =D
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

MrTAToad

You could probably do it INLINE, but in GLBasic you need to pass a defined type and not one that would be classed as "void".  SORTARRAY would is the only command that can be used to sort any type of array...

It should be noted that SORTARRAY uses quicksort, so for most cases is the fastest you can get...


Ian Price

I'm at work right now so can't test, but is SORTARRAY only for arrays containing values (numbers). Can it sort strings (text)too? Eg. re-order an array alphabetically?
I came. I saw. I played.

MrTAToad

Yes, you can use it for strings too

CatouSan

O.. K..  :'(

I thought there had to be away but nevermind, I'll be better off working with C++ in codeblocks. Thanks for your input guys.

kanonet

If i understood you right, you want to write function that you can use with SORTARRAY and that uses all types of arrays?
Sorry if i got you wrong, but i dont see your problem. If you want to sort different arrays with different types, why dont you just create different functions, one for each type? Yes it isnt a proper style of coding, but at least it works... When you want to sort a array, you already know what type it is, and so you know which of your function you need to use, so its easy to give SORTARRAY the right function pointer.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

CatouSan

SORTARRAY already has the ability to sort all different kinds of arrays, I just wanted to possibly create a library with all different kinds of sorts (for fun and experience mostly.. ). I can still make the library but I will only be able to sort arrays of integers or floats. I would have liked for it to be more flexible than that...

MrTAToad

Not limited to arrays of integers/floats.  You can sort arrays of types, extended types and strings...

You need a different comparison function for most of the, but its as flexible as anything else.

CatouSan

Wouldn't I have to duplicate and modify my existing functions to do that though?

MrTAToad

Iintegers and floats could use the same routine
Types and extended types could too, unless you call a routine inside the extended type.
Strings would need their own routine.