I am trying to write a version of bubblesort that will take in an array of strings and sort the strings. Each string has 15 characters of letters and numbers to compare as well as a name afterward. I want to be able to sort them based on all 15 characters as well as a range of characters (ie. characters 2-5)
void bubblesort(string data[], int lowbound, int highbound, int size)
{
int comparesize=highbound-lowbound+1;
char firststring[comparesize], secondstring[comparesize];
for (int i=size-1; i>=1; i--)
{
for(int j=0; j<i; j++)
{
if (lowbound==0 && highbound==14) //this part works
{
if (data[j]>data[j+1]) data[j].swap(data[j+1]);
}
else //this part doesnt
{
data[j].copy(firststring,comparesize,lowbound);
data[j+1].copy(secondstring,comparesize,lowbound);
if (firststring>secondstring) data[j].swap(data[j+1]);
}
}
}
}
when i sort based on all 15 characters, the string comparison operator (string1>string2) works fine. However, the only way I could think of comparing portions of the strings would be to copy the range of characters from each string and then compare those, but the string::copy member function only writes to arrays of chars, not actual strings. Is there an easier way to do this?