Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (henrybane), 1,182 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19053 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 5
Sorting strings alphabetically (several 'columns') #68147
03/25/06 05:59
03/25/06 05:59
Joined: Jul 2005
Posts: 366
eleroux Offline OP
Senior Member
eleroux  Offline OP
Senior Member

Joined: Jul 2005
Posts: 366
After lots of thinking and reading obtuse C++ STL libraries documentation... I decided to do this in C-script. It was far less difficult than I thought.
YES, it uses bubble sort, you code critiquing techie geek

This sorts two arrays of string pointers (vars), using the first one as sorting key. More secondary arrays (as virtual 'columns' in a spreadsheet) can be easily added to your needs, tho. The arrays should be the same size. The first array must be of string pointers. The others can be any type.

It could easily be ripped-off for number sorting, but I don't need that now.

Code:


// Sorting strings
// Coded by Emilio Le Roux
// This code is intended to sort several arrays, using the first as key

string cmpstring1;
string cmpstring2;

//returns true if str1 is alphabetically 'higher' than str2
function IsStringAfter(str1,str2)
{
//copies to temporary strings
str_cpy(cmpstring1,str1);
str_cpy(cmpstring2,str2);

//use only lower case for sorting
str_lwr(cmpstring1);
str_lwr(cmpstring2);

var counter=0;

var strlen;
strlen=min(str_len(str1),str_len(str2)); //loop up to the len of the shorter string
while (counter<strlen)
{

if (str_to_asc(cmpstring1) != str_to_asc(cmpstring2) )
{
//Return true if str1 is a higher asc value
return (str_to_asc(cmpstring1) > str_to_asc(cmpstring2) );
}
//Characters are equal: need to clip first character from both strings and try again
str_clip(cmpstring1,1);
str_clip(cmpstring2,1);

counter +=1;
}

//One of the strings is over and no results. So, the longer string must sort after the shorter one.
return (str_len(str1) > str_len(str2) );

}



//Swaps 2 strings' contents
string tempswapstr;

function SwapStrings(str1,str2)
{
str_cpy(tempswapstr,str1);
str_cpy(str1,str2);
str_cpy(str2,tempswapstr);

}



//Two or more arrays for the strings (pointers)
DEFINE MAXITEMS,15; //maxitems is your table 'ROWS'

string tempstring;

var arr_field1[MAXITEMS];
var arr_field2[MAXITEMS];


//sorts 2 arrays as fields, using array 1 as key. More arrays can be added (se below*)
function sortArrays(&array1,&array2) //may add more fields...*
{
var i;
i = MAXITEMS-1;
var j;
while(i>=0)
{
j=0;
while (j<i)
{
if (IsStringAfter (array1[j],array1[j+1]) )
{
SwapStrings (array1[j],array1[j+1]);
SwapStrings (array2[j],array2[j+1]);
//All additional arrays are swapped here*
}
j +=1;
}
i -=1;
}
}




//temp function to test the sort. Loads 2 tabbed columns from a txt file ( you can try pasting a 2-col Excel spreadsheet).

function sort_startup()
{

var fhandle;
fhandle = file_open_read("sorting.txt");
var counter = 0;
while (counter<MAXITEMS)
{
arr_field1[counter]=str_create("");
arr_field2[counter]=str_create("");

file_str_read(fhandle,arr_field1[counter]);
file_str_read(fhandle,arr_field2[counter]);

counter += 1;
}


sortArrays(arr_field1,arr_field2);


counter = 0;
while(counter<MAXITEMS)
{
draw_text(arr_field1[counter],10,counter*20,vector(200,100,200));
draw_text(arr_field2[counter],150,counter*20,vector(200,200,100));
counter +=1;
}

}



Re: Sorting strings alphabetically (several 'colum [Re: eleroux] #68148
01/03/07 10:44
01/03/07 10:44
Joined: Jul 2006
Posts: 503
Australia
A
adoado Offline

User
adoado  Offline

User
A

Joined: Jul 2006
Posts: 503
Australia
Nice!! Thanks!


Visit our development blog: http://yellloh.com
Re: Sorting strings alphabetically (several 'colum [Re: adoado] #68149
01/05/07 17:19
01/05/07 17:19
Joined: Mar 2006
Posts: 2,758
Antwerp,Belgium
frazzle Offline
Expert
frazzle  Offline
Expert

Joined: Mar 2006
Posts: 2,758
Antwerp,Belgium
Nice code eleroux
Can be helpfull to many users

Cheers

Frazzle


Antec® Case
Intel® X58 Chipset
Intel® i7 975 Quad Core
8 GB RAM DDR3
SSD OCZ®-VERTEX2 3.5 x4 ; HD 600 GB
NVIDIA® GeForce GTX 295 Memory 1795GB

Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1