Bạn thử cái này xem sao, nó có vẻ đơn giản :
PHP Code:
#include <iostream>
#include <string>
void StringSwap(char **, char **);
void StringSort(char **, const int , bool);
void PrintString(char **, int);
int main()
{
int choice;
const int size = 11;
char *Name[size] = { "Ronaldo", "Rivaldo",
"Beckham", "Zidane", "Figo", "Overmars",
"Chivu", "Redondo", "JaapStam", "Zanetty",
"Toldo"};
cout << "ORIGINAL \n";
PrintString( Name, size);
cout << "\nEnter choice :";
cout << "\n.1 Ascending, 0.Descending ";
cin >> choice;
StringSort( Name, size, choice);
cout << "\nAFTER SORTED \n";
PrintString( Name, size);
return 0;
}
void StringSwap(char **_first, char ** _second )
{
char *tmp = *_first;
*_first = *_second;
*_second = tmp;
tmp = NULL; /*Return pointer to NULL*/
}
void StringSort(char **A, const int sizeString, bool compare )
{
int order = 0;
for( int i = sizeString - 1; i > 0; i-- )
{
for( int j = 0; j < i; j++ )
{
order = strcmp(A[j], A[j+1]);
if(( compare && order > 0 )||(!compare && order < 0 ))
{
StringSwap( &A[j], &A[j+1] );
}
}
}
}
void PrintString(char **name, int sizeString )
{
for( int i = 0; i < sizeString; i++ )
{
cout << "[" << i+1 << "]" << name[i];
cout << "\n";
}
}