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;
}
}