How do I Custom Sort Directory Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonieko@hotmail.com

    How do I Custom Sort Directory Items

    I have a directory of files and I want to sort in in Array List in
    a customized way. How do I do that?[color=blue]
    >
    >
    >
    >
    >
    >[/color]
    class main
    {
    static void Main(string[] args)
    {
    string[] items = Directory.GetFi les( "c:\dir", "*.*");
    ArrayList al = new ArrayList( items);
    TransIdComparer Class myComp = new TransIdComparer Class();
    al.Sort(myComp) ;
    string topFile = (string) al[0];
    }
    }

    /// <summary>
    /// For example. This IComparer is used to sort based
    // on the fourth character and onwards in each filename.
    /// </summary>
    public class TransIdComparer Class : IComparer
    {
    // Calls CaseInsensitive Comparer.Compar e with the parameters
    reversed.
    int IComparer.Compa re( Object x, Object y )
    {
    string a = Path.GetFileNam e( (string) x);
    string b = Path.GetFileNam e( (string) y);
    return ( a.Substring(3). CompareTo(b.Sub string(3)) );
    }
    }

  • Ollie Riches

    #2
    Re: How do I Custom Sort Directory Items

    check out the IComparer interface, this is eactly what it is designed for:


    frlrfsystemcoll ectionsicompare rclasscompareto pic.asp

    example:

    public class myReverserClass : IComparer
    {
    // Calls CaseInsensitive Comparer.Compar e with the parameters reversed.
    int IComparer.Compa re( Object x, Object y )
    {
    return( (new CaseInsensitive Comparer()).Com pare( y, x ) );
    }
    }

    --
    HTH

    Ollie Riches


    Disclaimer: Opinions expressed in this forum are my own, and not
    representative of my employer.
    I do not answer questions on behalf of my employer. I'm just a programmer
    helping programmers.

    <anonieko@hotma il.com> wrote in message
    news:1109775594 .268802.136060@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > I have a directory of files and I want to sort in in Array List in
    > a customized way. How do I do that?[color=green]
    > >
    > >
    > >
    > >
    > >
    > >[/color]
    > class main
    > {
    > static void Main(string[] args)
    > {
    > string[] items = Directory.GetFi les( "c:\dir", "*.*");
    > ArrayList al = new ArrayList( items);
    > TransIdComparer Class myComp = new TransIdComparer Class();
    > al.Sort(myComp) ;
    > string topFile = (string) al[0];
    > }
    > }
    >
    > /// <summary>
    > /// For example. This IComparer is used to sort based
    > // on the fourth character and onwards in each filename.
    > /// </summary>
    > public class TransIdComparer Class : IComparer
    > {
    > // Calls CaseInsensitive Comparer.Compar e with the parameters
    > reversed.
    > int IComparer.Compa re( Object x, Object y )
    > {
    > string a = Path.GetFileNam e( (string) x);
    > string b = Path.GetFileNam e( (string) y);
    > return ( a.Substring(3). CompareTo(b.Sub string(3)) );
    > }
    > }
    >[/color]


    Comment

    Working...