Hi happy people
I've bin trying to sort items in a list box, the problem is obviously: It doesn't work
Please, any modifications to the code below is fully and unconditionally allowed.
void __fastcall TForm1::btnSortClick(TObject *Sender)
{
//sort ascending
if (rdAscending->Checked){
bool swopped = true;
int iEnd;
int arr = lstWords->Count;
//ShowMessage(arr);
iEnd = arr -1;
while (swopped)
{
swopped = false;
for (int c=0; c < iEnd; c++)
{
AnsiString a = lstWords->Items->Strings[c];
AnsiString b = lstWords->Items->Strings[c+1];
if (a > b)
{
AnsiString txt = lstWords->Items->Strings[c];
lstWords->Items->Strings[c] = lstWords->Items->Strings[c+1];
lstWords->Items->Strings[c+1] = txt;
swopped = true;
iEnd--;
}
}
}
'Let us do what we do best!'
}
//sort descending
else if (rdDescending->Checked){
bool swopped = true;
int iEnd;
int arr = lstWords->Count;
iEnd = arr -1;
while (swopped)
{
swopped = false;
for (int c=0; c < iEnd; c++)
{
AnsiString a = lstWords->Items->Strings[c];
AnsiString b = lstWords->Items->Strings[c+1];
if (a < b)
{
AnsiString txt = lstWords->Items->Strings[c];
lstWords->Items->Strings[c] = lstWords->Items->Strings[c+1];
lstWords->Items->Strings[c+1] = txt;
swopped = true;
iEnd--;
}
}
}
}
else
ShowMessage(
"Select either [Ascending] or [Descending]! \nPlease Try Again!");
}