Board index » cppbuilder » Customize SORT ORDER in TStringList ?

Customize SORT ORDER in TStringList ?

I need to customize the TSringList behavior .
How can I do that ?

Thanks.

Sam
___________________________

    Skne...@wanadoo.fr
___________________________

 

Re:Customize SORT ORDER in TStringList ?


Sam:

You'd have to turn off the Sort property and then use the OnChange event for
your custom sor, I believe.

John

--
Use file security for your password and regular applications. Find out how
at http://home.att.net/~special_projects/security.htm
It's Free!

Get The File Editor Professional
http://home.att.net/~special_projects/TheFileEditorProfessional.htm

Join the Shareware Authors Assessment Team see
http://home.att.net/~special_projects/home.htm for more information.

Quote
Sam <skne...@biosys.net> wrote in message

news:84j4u6$4df11@bornews.borland.com...
Quote
> I need to customize the TSringList behavior .
> How can I do that ?

> Thanks.

> Sam
> ___________________________

>     Skne...@wanadoo.fr
> ___________________________

Re:Customize SORT ORDER in TStringList ?


Quote
: "Sam" <skne...@biosys.net> wrote:
>I need to customize the TSringList behavior .
>How can I do that ?

Have a look at the documentation for the TStringList::CustomSort function.

You will need to create a standalone function, then pass this to
CustomSort; the string list, as part of sorting (...::Sort) will then call
your custom function and you can determine the ordering as you like.

--
General information:
  * Post to the right group - http://www.borland.com/newsgroups/
    * Do not cross- or multipost
      * Research at http://www.mers.com/searchsite.html

Stefan Hoffmeister - http://www.econos.de/
TeamB - http://www.teamb.com/

Re:Customize SORT ORDER in TStringList ?


I am sorry but TStringList::CustomSort is NOT AVAILABLE for the TStringList
class, only for TCustomListView, TCustomTreeView ans TTreeNode.

So how can I customize sort order in the TStringList ?

"Stefan Hoffmeister (TeamB)" <Stefan.Hoffmeis...@Econos.de> a crit dans le
message news: teaq6s44hucga9kh27bjcfs0q1m0oa6__BEGIN_MASK_n#9g02mG7!__...__END_MASK_i?a63jfAD$z__@4ax.com...

Quote
> : "Sam" <skne...@biosys.net> wrote:

> >I need to customize the TSringList behavior .
> >How can I do that ?

> Have a look at the documentation for the TStringList::CustomSort function.

> You will need to create a standalone function, then pass this to
> CustomSort; the string list, as part of sorting (...::Sort) will then call
> your custom function and you can determine the ordering as you like.

> --
> General information:
>   * Post to the right group - http://www.borland.com/newsgroups/
>     * Do not cross- or multipost
>       * Research at http://www.mers.com/searchsite.html

> Stefan Hoffmeister - http://www.econos.de/
> TeamB - http://www.teamb.com/

Re:Customize SORT ORDER in TStringList ?


Quote
On Fri, 31 Dec 1999 21:57:45 +0100, "Sam" <skne...@biosys.net> wrote:
>I need to customize the TSringList behavior .
>How can I do that ?

See my reply in

borland.public.cppbuilder.vcl.components.writing
Subject: Re: Where is the source code for TSTRINGLIST
Date: Fri, 31 Dec 1999 21:38:07 GMT

Ciao!

Re:Customize SORT ORDER in TStringList ?


Quote
: "Sam" <skne...@biosys.net> wrote:
>I am sorry but TStringList::CustomSort is NOT AVAILABLE for the TStringList
>class, only for TCustomListView, TCustomTreeView ans TTreeNode.

Whooops. Too much Delphi 5 recently. Sorry.

Well, in that case, you will have to create a descendant of TStringList
and override the Sort method, employing your own sorting algorithm.

--
General information:
  * Post to the right group - http://www.borland.com/newsgroups/
    * Do not cross- or multipost
      * Research at http://www.mers.com/searchsite.html

Stefan Hoffmeister - http://www.econos.de/
TeamB - http://www.teamb.com/

Re:Customize SORT ORDER in TStringList ?


Hi Sam,

Quote
> I need to customize the TSringList behavior .
> How can I do that ?

In addition to the excellent advice already posted, you can use a
temporary TList object and the TList::Sort() member function to achieve
custom sorting.  Here's a simple example...

int __fastcall SortFunction(void *item1, void *item2)
{
    // sort in descending order for example...
    return strcmp(static_cast<char *>(item2),
                  static_cast<char *>(item1));  

Quote
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TStringList *StringList = SomeStringList;
    std::auto_ptr<TList> List(new TList());

    for (int index = 0; index < StringList->Count; ++index)
    {
        AnsiString Entry(StringList->Strings[index]);
        char *entry = new char[Entry.Length()];
        strcpy(entry, StringList->Strings[index].c_str());
        List->Add(entry);
    }

    List->Sort(SortFunction);
    StringList->Clear();

    for (int index = 0; index < List->Count; ++index)
    {
        char *entry = static_cast<char *>(List->Items[index]);
        StringList->Add(entry);
        delete [] entry;
    }

Quote
}

Good luck!

Damon C.

Other Threads