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;
}

I cant figure out where you are wrong either. Could you please explain the exact problem you are facing, since your sort seems to be correct?!?!

If files in OpenDialog1 are all ready sort by Name than I get correct result for Sort, but if files in OpenDialog1 are sort by other type (for example by size) than I get other result.

I'm not sure that everything is OK, that's why I ask for some help.
Thanks in advance for your help.

Splitting it up into several functions (say one to determine the sort key(s), and one to do the actual sorting) would be a start.

250+ line functions are hard to read / test / debug.

try to create an array, single or multidimensional is your call. In the array, try to match the current value with the previous values, if it is greater than the previous, it goes to the end, other wise, you place it before the one bigger than it, and then shift the all the other items by one. Sorry for no codes, as m in a hurry but your question is dazzling me!! :)

Here's a sample code..

// using bubble sort algorithm

// components:
// 1. ListBox1
// 2. ListBox2

// "header.h"
// ...
class TForm1:public TForm
{
   // ...
   private:
   void __fastcall swap(int,int);
   void __fastcall sort(void);
   // ...
};

// "source.cpp"
// ...
void __fastcall TForm1::swap(int i1,int i2)
{
   AnsiString temp="";
   temp=ListBox1->Items->Strings[i1];
   ListBox1->Items->Strings[i1]=ListBox1->Items->Strings[i2];
   ListBox1->Items->Strings[i2]=temp;
}

//---------------
// ListBox2 items:
//---------------
// ABC.TXT
// CCCCMMYYYYSSDD.TXT
// 0404190300CCCC.TXT
// CCCCYYYYSSDDMM.TXT
// 0202190101CCCC.TXT
// CCCCSSDDMMYYYY.TXT
// 0101190002CCCC.TXT
// 123456.TXT

// Sorting...
void __fastcall TForm1::sort(void)
{
   ListBox1->Items=ListBox2->Items;
   int i_count=ListBox1->Items->Count;
   for(int i=n_size;i>=0;i--)
      for(int j=0;j<i_count-1;j++)
      {
         AnsiString t1=ListBox1->Items->Strings[j];
         AnsiString t2=ListBox1->Items->Strings[j+1];
         if(t1>t2)
            swap(j,j+1);
      }
}
This question has already been answered. Start a new discussion instead.