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 / Alphabetic sorting of Strings

Author
Message
ThePict
7
Years of Service
User Offline
Joined: 6th Nov 2017
Location:
Posted: 6th Nov 2017 21:21
How would I sort strings into alphabetical order with AGK2?
so I can compare two strings and determine whether they are alphabetically greater, lesser or identical than each other.
>, <, and = work with other BASICs, but will they here?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Nov 2017 08:33
They will work in AGK
But take a look in the help at Guides/Arrays in Version 2. There is a section on sorting which will help you.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
ThePict
7
Years of Service
User Offline
Joined: 6th Nov 2017
Location:
Posted: 7th Nov 2017 19:37
Thanks, that helps a bit.
I've only just started to dip into AppGameKit, but it looks a lot easier to learn (I'm an old BASIC 8-bit guy) than the others I've dabbled with.

I had a bit of old code from BlitzBasic that converted words into base-26 integers (had to reverse the word if I'm remembering right) making alphabetical sorting very easy, but limited the word size to fit within the 32-bit confines of the integer. 64-bit would all but eliminate that, but I'm not sure AGK's Integers are 64-bit capable. I did see a function that converted text to 64-bit, but not tried that yet.
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 7th Nov 2017 21:19 Edited at: 8th Nov 2017 07:34
Unless sorting a one-dimensional array of strings, integers or floats, you're going to have to roll your own.

The good news is that simple compare of strings to find if one is alphabetically before (or after) the other will work. Which makes it a bit easier.

Here's a selection-sort routine I made for an earlier project to sort a list of users. This one got two modes though, sort on user name and sort on user ID.



For larger lists, you should use a more efficient sort-algorithm, like quick-sort. But for a list of up to maybe a thousand, this selection-sort method will do just fine.

edit: As PSY have teached me something new today, you can also sort one-dimensional arrays of strings - so added edited that in.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 8th Nov 2017 02:10 Edited at: 8th Nov 2017 02:10
names as string[5] = ["Peter", "Mandy", "Tina", "Anna", "Barbara"]

names.sort() will sort the array correctly
PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-11-24 09:05:58
Your offset time is: 2024-11-24 09:05:58