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 Classic Chat / can i sort types further when the 1st variables are equal

Author
Message
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 30th Jul 2017 12:18 Edited at: 30th Jul 2017 13:15
Taken from AGK2 guide "When sorting types the first variable of the type will be used to compare elements."

However the 2nd variable does not continue this sorting procedure, so when 2 type variables are equal, how do i further sort on a 2nd type variable?

The following example shows the 1st type variable as "score", the 2nd type variable as "name". It sorts correctly on the variable "score" however there does not appear to be any further sorting thereafter.



So as you can see after running this, its sorted on "score" but how would i then for example further sort on "name".

It would be great if it further sorted on the type variables in the order they are placed in the "type".
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 30th Jul 2017 21:38
I think you may need to create a "key" which is a combination of the two. It would be nice if it did this though. Maybe ask in the V2 feature requests thread
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 31st Jul 2017 00:20
as blink0k mentioned
if the first type entry is string then the sort string looks like
001231Berta
000001Walter
000001Anton
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 31st Jul 2017 02:21
I wonder what would happen if you created a type which consisted of the fields you want as keys
and then use that type as the first entry in your array. like this;

type myKey
score as intteger
name as string
endtype

type myType
key as myKey `it sorts on this variable
ID as integer
endtype
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 31st Jul 2017 02:38
Had to solve the same problem a few years back using AGK.

Was for an app to show scores and such for horse-showjumping. Showed them the high-score list function, whereupon they observed that it only sorted on score.

"And that is a problem?" I asked.
"Well yes!" was the reply.
"Why? Can't the winners podium fit more than one horse?"
"We don't put the horses on the winners podium!"
"So what's the problem?!"

My reasoning was not well received. They insisted on sorting on score first, time second. So if two or more had the same score, they'd be differentiated on time to finish.

That required a bit of head-scratching, a pot of coffee and two large Red Bull before a working solution could be presented the morning after.

After the initial sort based on score - the list had to be iterated over. Where two scores was equal, it'd note the first index, and add to an accumulator until it encountered a score that was higher. Then the list from index of first equal index to first equal + accumulator index was sent to a function, sorted on time and reinserted into the complete list.

I did not use the built in sorting function, but my own home-cooked selection-sort routine. Not great on big datasets or partially sorted lists, but one of the best on entirely unsorted lists of small to medium size. Also, easy to implement.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 31st Jul 2017 03:47
Ok. my idea (re: a type as the first value) wont work.
returns the error
main.agc:42: error: Can only sort arrays of types if the first type variable is an Integer, Float, or String
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 31st Jul 2017 11:18
Thanks guys, i posted in the request thread, eventually, so fingers crossed its an easy one to add next time.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 13th Sep 2017 05:39
You could try a concatenation of the strings being the key and use this in the sort
Like Markus is suggesting but bit different
Ie ScoreBenID

A string for the score where you trail it with 0 for a certain max score numbers
the name id could have concatenated space characters for ease of comparison

Join these both together when you create an id for that person
It should be unique allot of the time using this method if for some reason it conflicts
change the space at the end to a 1 etc or force the user to reenter there name

so id string may be
0000020210Tom
0000010010Bob
0000010010Bob 1

The code for leading with zeros should not be hard just need to know how many thousand etc the max expected score to be



fubar
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 13th Sep 2017 07:07
I mean, you could write your own sort function and have it sort by whatever.
Battoad
AGK Developer
17
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 18th Sep 2017 10:38
Thanks for inputs,appreciated.
@fubarpk nice idea will give it a try.
@IronManhood yes writing my own sort function should be a last resort.

Dissapointed that the current sort array command is basically quite useless if it is not going to be extended to further sort on optional type variables.
Perhaps it should be removed altogether if we have to write our own.

I have requested for this commnd to be extended in both the V2 request feature thread and the recent survey some of us may have completed, but without any feedback who knows what we can expect to be added or how long we may have to wait. For me the lack of feedback or a roadmap is quite discouraging and therefore provides limitations on what can be achieved within our own capabilities.
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 18th Sep 2017 17:36
Fubars solution should work. You could also just get the biggest score first, turn it into string, count the chars, then diff the remaining scores for the length. Prefix by zeros diff times for each score. There is the sort key. No need to guess max value. Except maybe max integer value is still a limit but anyway..

Login to post a reply

Server time is: 2024-03-29 13:57:31
Your offset time is: 2024-03-29 13:57:31