Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / [SOLVED] Problem with myArray.sort()... I want to sort integers from low to high, but does not work.

Author
Message
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 16:45 Edited at: 12th Mar 2023 17:56
I would like to sort variables, (integers), from low to high, but my coding does not work.
I figured that sort(array) would be an ideal method... but I don't think it accepts variables.


Mark G.
Coding is bliss...
Sometimes

The author of this post has marked a post as an answer.

Go to answer

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 18:54
This one will sort by ID:




This one will sort by name:


James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 12th Mar 2023 19:10
3 points;
1) you have made 2 typo's on line 40
2) you can't use index 0 , only hint of this I could find in help files was "some people used to use index 0 to store the length of the array for convenience, but with the new changes this is no longer necessary" which suggests to me to leave 0 alone just in case, just before this info we are also instructed that for simplicity to use from index 1
3) the sort function only affects the first variable of a type - in this case the sort function is applied to "ID" not "name"

@steve that does not compile for me, are you using classic or maybe older version of studio or even tier2 or vscode? Compile error is on the sort function which would match the help files info for enhanced arrays
Win 10 Home 64, Intel(R) Core(TM) i5-10300H, 8GB DDR4, NVidia GeForce GTX 1650 4GB GDDR6
JosephB
17
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 12th Mar 2023 20:18
All,

I still do not quite follow the new array system, but to sort the array types it does not work well if you go through all the indexes. In this case the array length is five, but the last array index is 4 (0 to 4) for a length of 5. The following compiles and sorts by myArray.name in AGK2 Classic:

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 20:40
@James H
I'm using AppGameKit Studio.
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 20:43
@James H
Don't always trust copy/paste as I've noticed code doesn't always compile when that is used but then does compile when I type it manually. I think hidden chars within the code boxes on here might be including hidden format chars.
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 20:48
@James H
I've just tried it with both AGK2 and Studio and both are compiling while using copy/paste, I think something may be wrong your end in this case mate. Check that you haven't accidently bumped a random key and caused a typo somewhere.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 20:57 Edited at: 12th Mar 2023 20:59
@James H
"3) the sort function only affects the first variable of a type - in this case the sort function is applied to "ID" not "name""

Yeah that's right, I actually forgot about that until you mentioned it.


"2) you can't use index 0"

I've always searched and sorted from base zero and I even tried it a few minutes ago and it works fine. As long as you don't go into minus values then there's no problem.
adambiser
AGK Developer
8
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 12th Mar 2023 21:04 Edited at: 12th Mar 2023 21:04
When sorting an array of types, array.sort() uses the first member of the type for sorting.

Using index 0 is fine and usually necessary. Array indices are 0-based.

AGK, however, messed up its concept of array length and almost everywhere you see length for arrays, they really mean upper bound, ie: index of the last item.
ie:
Given myArray as myType[5]
there are 6 items in the array, indices 0 through 5.
myArray.length reports 5, but that's really the upper bound, not the length. To iterate over the indices, you need to go from 0 to myArray.length + 1.

So when you have myArray as myType[5] and only set indices 0 to 4, the values at index 5 are 0 and sort first.
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 21:05 Edited at: 12th Mar 2023 21:09
You can also sort multiple associated arrays that need to retain the position for the other array(s) when the primary array is sorted, in this example I'm sorting names into alphabetical order while still keeping the ages with the intended names.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 21:15
How I forgot that the first field in a UDT is sorted by the built in array.sort() function is was I find embarassing, somebody please give my brain a wobble.
James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 12th Mar 2023 21:34
@steve yeap - copy/paste was only sort of the issue - but I think it was the loading of the snippet/web page...so basically I used the copy button last time around - but I do recall there being no scrolling available on the snippet when i was reading it AND my last compile was your snippet - which showed no sort function either - just ended after Loop - which now when I open the snippets they end on Loop without scrolling, obviously you haven't edited your post either. This time around there is said function/scrolling available. So maybe there is a common issue with loading of snippets to the page that has gone unnoticed somehow. I genuinely did think there was an old way of using arrays when I saw your function call, thought you had just messed up myArray.sort() or that Sort(myArray) was an older way!!!! In my head at that point I was thinking huh?? How does changing locals to global and ensuring a random value greater than zero suddenly and magically sort the next variable?? Pmsl my sides hurt from laughter. Mad thing is the first snippet of bubbas must have loaded/copied correctly as there where no compilation issues/ran as expected, so maybe it works fine just the once lol. Oh well, definitely keeping my eye on copy/past from now on regardless! Cheers
Win 10 Home 64, Intel(R) Core(TM) i5-10300H, 8GB DDR4, NVidia GeForce GTX 1650 4GB GDDR6
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 21:41 Edited at: 12th Mar 2023 22:56
This worked very good, shows ascending values. But how would I print the name next to the values, such as
(for example).. 20.24.56.58.92 into c 20, b 24, a 56, e 58, d 92
like, keeping them in ascending order, but also showing the name next to the values? When it 'prints out' such names ( a,c,e,d,b)
And I can make my game even better now. (More professional looking)
But, never the less, that is immense help and I found out I can print the names in separate column below the ascending values. as such:

Mark G.
Coding is bliss...
most of times.

James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 12th Mar 2023 22:05
Okay I get why you folks keep stating about index zero...however when sorting is applied index zero can lose its currently held value, rem in/out the sort function in this code:

Win 10 Home 64, Intel(R) Core(TM) i5-10300H, 8GB DDR4, NVidia GeForce GTX 1650 4GB GDDR6
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 22:12 Edited at: 12th Mar 2023 22:13
There is a length of 5 but there are 6 elements, you can actually put stuff into myArray[5]. The length of the array is also the final index.
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 22:47 Edited at: 12th Mar 2023 22:48
Thank you Steve Arnell and James H !
Mark G.
Coding is bliss...
most of times.

James H
16
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 12th Mar 2023 22:49
@steve hence why I get it - which i have since the start...this is about the OP as opposed to just the info there after - it has not been clearly stated enough i feel about the confusions around this. to the point that the code provided by OP is using index zero to store info - see how the OP code reports a zero to screen even though the minimum value by random function is 1 and ofc index zero being used to store this. This is not changed in your own code in your first responses, any explanations thereafter have not touched upon this point to the point of clarity or more especially the need for attention being drawn to this point. I was concerned this had not transitioned across which is reinforced by the fact that nobody has mentioned this risk on index zero and as per the OP's most recent post, index zero is still being used to store data when it will definitely get wiped and index 5 is not(and must be feeling lonely by now), hence the code still produces a value that should never have been produced given the minimum should have been 1 not 0, therefore the point needs reiterating once more don't you think?
Win 10 Home 64, Intel(R) Core(TM) i5-10300H, 8GB DDR4, NVidia GeForce GTX 1650 4GB GDDR6
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 23:01 Edited at: 12th Mar 2023 23:24
Yes, your right, I noticed that slight mistake of Steve, and I fixed it right away.

Here is my final code:
Mark G.
Coding is bliss...
most of times.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 23:06
Don't get me wrong I understand your point quite clearly, that's why when I clear an array I always set its length to minus 1. I also favour declaration of an array as myArray as MyType[], and using myArray.insert(value) when assigning data to it, rather than setting its length in the declaration.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 23:06
@Bubba
You're welcome mate.
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 23:09 Edited at: 12th Mar 2023 23:13
Really Steve, that was a genius solution of yours
Especially with the function you provided... Sort(this ref as MyType[])
I spent hours trying to figure out how to do that, and you saved me gobbs of time.
Mark G.
Coding is bliss...
most of times.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 23:12
Bottom line is it doesn't matter who's wrong or right though. I may have came up with a solution but that doesn't always mean my way is the bext way, James H still bought up a valid point and jogged my memory about the first element in a UDT and adambiser confirmed that fact. Bubba also got to learn a bit more and might even surpass a few of us later on down the line, it's all win-win!
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 23:15 Edited at: 12th Mar 2023 23:16
@Bubba

I forgot to clarify what the "ref" is in Sort(this ref as MyType[]). If you don't include ref a completely new copy of the instance of the object parsed is created and worked on inside the function, with ref it modifies the actual instance that was already created.

I hope I explained that well enough, if anyone else can explain it in a clearer and better way then please feel free to pipe up.
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Mar 2023 23:44 Edited at: 13th Mar 2023 05:09
My game is a 'one player' game in which the player plays against the computer, and tries to get as much points as possible,

Mark G.
Coding is bliss...
most of times.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 12th Mar 2023 23:49
@Bubba
That sounds pretty cool.

I'm not sure if you already knew what the "ref" keyword is for but I will give you these two examples just in case. I hope you find this useful.


The oldskool return a copy of data.




And the "ref" way of doing it.


Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 13th Mar 2023 00:01
@Bubba
"Maybe if there was someone I could trust, I could get all those 'bells and whistles' figured out.
Is there anyone you know I could pass it to for finishing?"

If you mean like graphics and sound you might be better off outsourcing on one of those freelance sites, or ask a few forums or Facebook group, I personally just use around the house items for making sounds but my art skills are way beyond crap.

And what ever you do never hand over the complete project such as sourcecode, unless you know them first hand and know you can trust them.


On the subject of my crap art skills here's a few screenshots of my current project.




Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 13th Mar 2023 00:07
Oh, that looks good, for sure.
Mark G.
Coding is bliss...
most of times.

Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 13th Mar 2023 00:12 Edited at: 13th Mar 2023 00:31
Oh, well, have a nice day. Nice meeting you.

Mark G.
Coding is bliss...
most of times.

Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 13th Mar 2023 00:21
I released this one to the public last year. https://steve-ancell.itch.io/the-need-for-friction
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 13th Mar 2023 00:31 Edited at: 13th Mar 2023 00:45
This post has been marked by the post author as the answer.
That looks nice. Interesting.

Mark G.
Coding is bliss...
most of times.

Login to post a reply

Server time is: 2024-04-20 04:06:53
Your offset time is: 2024-04-20 04:06:53