Sorting StringLists....

Discuss and publish sample code and applications written in OrbC (OrbForms Designer or PocketC Architect)

Postby wildangus on Sun Apr 18, 2004 4:39 am

For all who need to have sorted lists - here's how -
Include the code in your projects editor and just call sortlist with the name of the stringlist to sort.
Don't call qsort directly - it's just called from sortlist.

Adapted from the Turbo Pascal quicksort demo code... so it probably could be optimed with ++ increments and suchlike C constructs or I suspect the swap code could be optimised... but it works fine for me... your mileage may vary...

Usual caveats - no warranty, no liability, cars parked at owners risk, may contain nuts, indentation is lost when posting code.


void qsort(StringList sortlist, int l ,int r) {
int i,j;
string x,swap1,swap2;
i=l;
j=r;
x=sortlist.item((l+r) / 2);
do {
while (sortlist.item(i) < x) i=i+1;
while (x < sortlist.item(j)) j=j-1;
if (i <= j) {
swap1=sortlist.item(i);
swap2=sortlist.item(j);
sortlist.del(i);
sortlist.insert(i,swap2);
sortlist.del(j);
sortlist.insert(j,swap1);
i=i+1;
j=j-1;
}
}
while (j>=i);
if (l<j) qsort(sortlist,l,j);
if (i<r) qsort(sortlist,i,r);
}

void sortlist(StringList mylist) {
qsort(mylist,0,mylist.count-1);
}



Wild Angus }:-)
Wild Angus }:-)
wildangus
 
Posts: 7
Joined: Sun Nov 17, 2002 7:23 pm
Location: United Kingdom

Re: Sorting StringLists....

Postby earlcasper on Fri Jan 25, 2008 12:07 am

Pocket C Architect now includes the function StringList.sort(). :)
earlcasper
 
Posts: 25
Joined: Thu Sep 29, 2005 11:50 pm
Location: USA


Return to OrbC User Samples

Who is online

Users browsing this forum: No registered users and 1 guest

cron