Hello everybody.
I'm new in C++ Builder and I have problem with my application.
I have some files on input and first I add files in ListBox1. I try to sort them by 3 conditions (firts is date, second is some secc. number and thirt is const. number) for example filename is eCCCCCCddmmyyyyssnnn
I want to sort files in ListBox firts by ddmmyyyy then by ss and on the end I put firts files with some CCCCCC then with other CCCCCC.
I do something but it doesn't work. Here is my code for this function. Can you please help me :(
AnsiString __fastcall TfrmCCounter::FileSorted()
{
int brFiles = 0;
int dol =0;
int Brojac = 0;
int i=0;
AnsiString FileName="";
AnsiString BIN = "";
AnsiString prv = "";
AnsiString pom="";
AnsiString datum = "";
AnsiString brojce = "";
AnsiString dir = "";
AnsiString strQuery = "";
AnsiString text = "";
AnsiString NajmalBroj = "";
AnsiString NajmalBrojPom = "";
bool prefrleno = false;
bool ImaPomalDatum = false;
bool ImaPomalDatumPom = false;
ListBox1->Clear();
ListBox2->Clear();
if (OpenDialog1->Execute())
{
StatusBar1->Panels->Items[1]->Text= "";
StatusBar1->Refresh();
brFiles = OpenDialog1->Files ->Count;
for (int i=0; i<brFiles; i++)
{
dol = OpenDialog1 ->FileName.Length();
FileName = OpenDialog1 ->Files->operator [](i);
dir = FileName.SubString(0,dol-19);
FileName = FileName.SubString(dol-19,20);
BIN = FileName.SubString(2,6);
Query1->Close();
Query1->SQL->Clear();
strQuery = "select * from tblCCount where BIN='" + BIN + "'";
Query1 ->SQL->Text = strQuery;
Query1->Active = true;
if (!(Query1->Eof && Query1->Bof))
{
ListBox1 ->Items ->Add(FileName);
if (NajmalDatum == "")
{
NajmalDatum = FileName.SubString(8,8);
}
else
{
if (NajmalDatum.ToInt() > FileName.SubString(8,8).ToInt())
{
NajmalDatum = FileName.SubString(8,8);
ImaPomalDatum = true;
}
}
ListBox1 -> Refresh();
}
else
{
text = "Imeto na EMBOSS-ot " + FileName + " ne e vo red";
StringGrid2->Visible = true;
StringGrid2 -> Cells[0][StringGrid2->RowCount-1] = text;
StringGrid2 -> RowCount++;
StringGrid2 -> Refresh();
i++;
}
}
Brojac = ListBox1 -> Items -> Count;
i = 0;
if (ImaPomalDatum == true)
{
while (i<Brojac)
{
prv = ListBox1 -> Items -> operator [](i);
prefrleno = false;
if (prv.SubString(8,8) == NajmalDatum)
{
ListBox2 ->Items -> Add(ListBox1 -> Items -> operator [](i));
ListBox1 -> Items -> Delete(i);
ListBox1 -> Refresh();
ListBox2 -> Refresh();
Brojac--;
prefrleno = true;
}
if (prefrleno == false)
{
i++;
if (NajmalDatumPom == "")
{
NajmalDatumPom = prv.SubString(8,8);
}
else
{
int p = prv.SubString(8,8).ToInt();
int q = NajmalDatumPom.ToInt();
if (q > p)
{
NajmalDatumPom = prv.SubString(8,8);
ImaPomalDatumPom = true;
}
if (ImaPomalDatumPom == false)
{
NajmalDatumPom = prv.SubString(8,8);
}
}
}
else
{
Brojac = ListBox1 -> Items -> Count;
prefrleno = false;
}
if (i == ListBox1->Items->Count)
{
i = 0;
Brojac = ListBox1->Items->Count;
NajmalDatum = NajmalDatumPom;
ImaPomalDatumPom = false;
}
}
}
i = 0;
for (int a=0; a <ListBox2->Items->Count; a++)
{
ListBox1 ->Items -> Add(ListBox2 -> Items -> operator [](a));
ListBox1->Refresh();
}
ListBox2->Clear();
/*****************************************
sort first by 532541
second by 536506.
*****************************************/
Brojac = ListBox1 -> Items -> Count;
i = 0;
while (i < Brojac)
{
prv = ListBox1 -> Items -> operator [](i);
BIN = prv.SubString(2,6);
prefrleno = false;
if (BIN == 532541)
{
ListBox2 ->Items -> Add(ListBox1 -> Items -> operator [](i));
ListBox1 -> Items -> Delete(i);
ListBox1 -> Refresh();
ListBox2 -> Refresh();
Brojac--;
prefrleno = true;
}
if (prefrleno == false)
{
i++;
}
else
{
Brojac = ListBox1 -> Items -> Count;
prefrleno = false;
}
}
Brojac = ListBox1 -> Items -> Count;
i=0;
while (i < Brojac)
{
prv = ListBox1 -> Items -> operator [](i);
BIN = prv.SubString(2,6);
prefrleno = false;
if (BIN == 536506)
{
ListBox2 ->Items -> Add(ListBox1 -> Items -> operator [](i));
ListBox1 -> Items -> Delete(i);
ListBox1 -> Refresh();
ListBox2 -> Refresh();
Brojac--;
prefrleno = true;
}
if (prefrleno == false)
{
i++;
}
else
{
Brojac = ListBox1 -> Items -> Count;
prefrleno = false;
}
}
/*************************************************
Sort firts by BIN then by Date (IT MISS BY ss - )
**************************************************/
Brojac = ListBox2 -> Items -> Count;
while (Brojac != 0)
{
i = 0;
prv = ListBox2 -> Items -> operator [](0);
datum = prv.SubString(8,8);
BIN = prv.SubString(2,6);
while (i< Brojac)
{
prefrleno = false;
pom = ListBox2 -> Items -> operator [](i);
if ((pom.SubString(8,8) == datum) && (pom.SubString(2,6) == BIN))
{
ListBox1 ->Items -> Add(ListBox2 -> Items -> operator [](i));
ListBox2 -> Items -> Delete(i);
ListBox2 -> Refresh();
ListBox1 -> Refresh();
Brojac--;
prefrleno = true;
}
if (prefrleno == true)
{
i = 0;
}
else if (prefrleno == false)
{
i++;
}
}
}
} //if (OpenDialog1->Execute())
ListBox1 -> Visible = true;
StatusBar1->Panels->Items[1]->Text= "Zavrsi podreduvanjeto na emboss-ite";
StatusBar1->Refresh();
return dir;
}