GLBasic forum

Main forum => GLBasic - en => Topic started by: CatouSan on 2011-Nov-26

Title: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-26
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?
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-26
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
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
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.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: Hatonastick on 2011-Nov-27
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. :)
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
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.



Title: Re: ADDRESSOF and SORTARRAY questions
Post by: Hatonastick on 2011-Nov-27
Erm... Kitty?  Mr TA Toad?  This is _all_ yours!  =D
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
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...

Title: Re: ADDRESSOF and SORTARRAY questions
Post by: Ian Price on 2011-Nov-27
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?
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
Yes, you can use it for strings too
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
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.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: kanonet on 2011-Nov-27
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.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
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...
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
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.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
Wouldn't I have to duplicate and modify my existing functions to do that though?
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
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.


Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
How could arrays of types and extended types use the same routine as arrays of ints and floats? I have been pulling out my hair trying to figure that out..
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
No, they would use a separate routine.

Integers/floats would use one routine
Types use another
Strings would use another one

It would be possible only three sort routines would be needed.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-27
I see. So when you say types use one routine, are you referring to all types? Ex. I can use one function to sort an array of dogs, and later an array of cats?
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-27
Yes, I dont see why not unless the arrays have different type structures...
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: Moru on 2011-Nov-28
You need one routine for each type structure. TYPE Cats need one routine, TYPE Dogs need another routine. Ofcourse, the comparison would have been easier to not misunderstand if you had used TYPE Animals and TYPE Cars. Or something :-)
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: MrTAToad on 2011-Nov-28
Or you could have a type of TAnimals and then an array for cats and dogs...
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-28
You guys gave me some ideas I will apply to create a solution, it will be a pain, but it will also be elegant.

Thanks again  :)
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: Kitty Hello on 2011-Nov-28
SORTARRAY uses the < (less than) operator of an array's element. So, if you have integers, floats or strings, they are sorted automatically.
The TYPE arrays have a built-in less-than operator that works by the order you write the type members top to bottom.
If you want a different sorting order (e.g. sort numbers by the length when you print them), you have to provide a sorting function.
Title: Re: ADDRESSOF and SORTARRAY questions
Post by: CatouSan on 2011-Nov-29
Quote from: Ocean on 2011-Nov-28
Quote from: CatouSan on 2011-Nov-28
You guys gave me some ideas I will apply to create a solution, it will be a pain, but it will also be elegant.

Thanks again  :)

how can something elegant e a pain?
Elegant in how it works, a pain to write.   :P