# Highscores with nicknames - Natural Sort?

0 Favourites
• 11 posts
• Hey, I want to save the highscores with corresponding nicknames.

At the moment I have an array (1d) with 10 elements filled with strings like:

"520 | Nickname1" ,

"40 | Nickname2",

"270 | Nickname3" ,

"25 | Nickname4",

"120 | Nickname5", etc.

The built-in sort function sorts it like this. And it should be like this:

"520 | Nickname1" ,

"270 | Nickname3" ,

"120 | Nickname5",

"40 | Nickname2",

"25 | Nickname4", etc.

To sort it properly, i must convert all the elements to int(), but then i lost the nicknames. I've found something like Natural Sort [argh, i can't post links yet... "array natural sort" in Google -> first link], but is there any way to implement this in Construct 2?

Or maybe is there a tutorial how to save the highscores WITH nicknames?

• When you are collecting your score/names why not bulk out your score so it becomes "000123" or "008765" rather than "123" and "8765" then ,I think, the sort will work.

• ok after a quick test I am a bit more confident that it would work.

I thought you would have to write a routine to pad out the numbers but there is a text option/ expression called zeropad

Pad number out to a certain number of digits by adding zeroes in front of the number, then returning the result as a string. For example, zeropad(45, 5) returns the string "00045"."

and a quick capx (using beta 155)

• Thanks, good idea! It's working, but now i can't figure out how to 'un-zeropad' these scores just before putting them in textbox (to make it look nice, so "00070" will be "70", etc.)

I've tried setting text to "tokenat(Array.CurValue, 2, "0") & newline", but the number of left-side-zeros varies + the scores have zeros too = not working...

(Maybe some loop checking the nth character in the string, starting from the left and IF nth-character == "0" change it to "", ELSE end the loop + trim() it at the end? But i don't know how to check n-th character of the text in C2)

• here is a slight edit

just take the array value into a variable and it will autmatically remve the zeros.

edit - and it looks better with the leading 0s. (in my (not very humble) opinion) <img src="smileys/smiley1.gif" border="0" align="middle">

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Yeah but when the array contains elements with nicknames (like "00750 | BobTheDarkLord", putting it to variable deletes the nickname :(

Hmm I'll consider leaving it with the leading 0's, but it just fits better to my game's visual style without them.

• I have high scores in my demo project which should be done in a month or so. If you're saving them on a server and accessing them via an Ajax / PHP interface sorting them server-side is trivial (e.g. SELECT * FROM highscores ORDER BY score)

If you're doing it in Construct 2 I'd try using a selection sort; it's really good on platforms where your ability to directly manipulate memory elements is limited since it sorts the list in-place.

• sorry - I missed the replies.

you don't need to loose the names - I will try to give you an example but it won't be for 24 hours at best.

• More time than I thought !

sort array mk 2

• Why not use a 2d array?

If you can only save a string for each highscore, convert the array to such a string when saving and convert the string to the array when loading.