Two-Dimension array sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Wasser

    Two-Dimension array sort

    I need to sort a two-dimensional array. Each day I process a file with 9
    comma-delimited fields, and varying amount of records (lines). I want to
    pull in each line, split it according to the comma into a two dimensional
    array (Perl has it f'r crissake), and sort by one of the fields (Purchase
    Order #). Trouble is, Array.Sort only supports single dimension arrays.
    Originally I was thinking of making a jagged array, pulling out the Purchase
    Order element, assigning it to a key value corresponding to the single array
    line, sort and re-construct the lines, sorted. Easy to say in English, not
    so easy for a C# noob.

    --
    Steve Wasser

    the journal of contrarian social discourse and neurotic opinion


  • Hector Martinez

    #2
    RE: Two-Dimension array sort


    I'm sorry, but I can't understand why you need a two-dimensional array...

    Can you give me a better explanation

    Comment

    • David Browne

      #3
      Re: Two-Dimension array sort


      "Steve Wasser" <sewasser@hotma il.com> wrote in message
      news:1005rrobld 38c0f@corp.supe rnews.com...[color=blue]
      > I need to sort a two-dimensional array. Each day I process a file with 9
      > comma-delimited fields, and varying amount of records (lines). I want to
      > pull in each line, split it according to the comma into a two dimensional
      > array (Perl has it f'r crissake), and sort by one of the fields (Purchase
      > Order #). Trouble is, Array.Sort only supports single dimension arrays.
      > Originally I was thinking of making a jagged array, pulling out the[/color]
      Purchase[color=blue]
      > Order element, assigning it to a key value corresponding to the single[/color]
      array[color=blue]
      > line, sort and re-construct the lines, sorted. Easy to say in English, not
      > so easy for a C# noob.
      >[/color]


      Here's an example of how to sort a jagged array. To sort any array, you
      just need to tell the framework how to perform pariwise comparisons on the
      elements. This is done by supplying a type implementing IComparer.

      Here an IComparer called ArrayComparer compares 1-dimensional arrays by
      comparing elements at some particular index.

      public class ArrayComparer : System.Collecti ons.IComparer
      {
      int ix;
      public ArrayComparer(i nt SortFieldIndex)
      {
      ix = SortFieldIndex;
      }

      public int Compare(object x, object y)
      {
      IComparable cx = (IComparable)(( Array)x).GetVal ue(ix);
      IComparable cy = (IComparable)(( Array)y).GetVal ue(ix);
      return cx.CompareTo(cy );
      }
      }


      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {

      string[][] lines = new string[4][];
      lines[0] = new string[] {"1","a","d" };
      lines[1] = new string[] {"2","b","c" };
      lines[2] = new string[] {"3","c","b" };
      lines[3] = new string[] {"4","d","a" };

      foreach (string[] line in lines)
      {
      Console.WriteLi ne(line[0]);
      }
      System.Array.So rt(lines,new ArrayComparer(2 ));
      foreach (string[] line in lines)
      {
      Console.WriteLi ne(line[0]);
      }

      }

      David


      Comment

      • Eric Gunnerson [MS]

        #4
        Re: Two-Dimension array sort

        One way to do this is to create a class that represents each row of the
        data, and then have it implement IComparable on the field on which you wish
        to sort. You can then keep instances of the class into an arraylist, and
        call sort.

        If you wanted, that class could store things internally in an arraylist.I
        would probably go for more descriptive field names.

        Oh, and call String.Split() to get an array from the comma-delimited fields.

        --
        Eric Gunnerson

        Visit the C# product team at http://www.csharp.net
        Eric's blog is at http://weblogs.asp.net/ericgu/

        This posting is provided "AS IS" with no warranties, and confers no rights.
        "Steve Wasser" <sewasser@hotma il.com> wrote in message
        news:1005rrobld 38c0f@corp.supe rnews.com...[color=blue]
        > I need to sort a two-dimensional array. Each day I process a file with 9
        > comma-delimited fields, and varying amount of records (lines). I want to
        > pull in each line, split it according to the comma into a two dimensional
        > array (Perl has it f'r crissake), and sort by one of the fields (Purchase
        > Order #). Trouble is, Array.Sort only supports single dimension arrays.
        > Originally I was thinking of making a jagged array, pulling out the[/color]
        Purchase[color=blue]
        > Order element, assigning it to a key value corresponding to the single[/color]
        array[color=blue]
        > line, sort and re-construct the lines, sorted. Easy to say in English, not
        > so easy for a C# noob.
        >
        > --
        > Steve Wasser
        > http://xdissent.com
        > the journal of contrarian social discourse and neurotic opinion
        >
        >[/color]


        Comment

        • Hector Martinez

          #5
          Re: Two-Dimension array sort


          I think what you say is his more elegant option...

          Comment

          • Steve Wasser

            #6
            Re: Two-Dimension array sort

            Think of what I am processing as a datagrid, only not coming from a
            database, it gets sent as a text file each day. Like:

            12/30/2003,564,86827, 4212672,27082,N SB,5000
            12/30/2003,564,86827, 4212672,27082,S X1,1500
            01/01/2004,396,88513, 4220205,32262,M M6,1500
            01/01/2004,396,88513, 4220205,32262,N M6,7000
            01/02/2004,302,88513, 4216938,22837,M M6,1200
            01/02/2004,302,88513, 4216938,22837,N M6,5500
            01/02/2004,302,88513, 4216938,22837,P M6,2000

            I need to sort according to the 4th field. I was using StreamReader.Re adLine
            to pull it in. Because I have to compare the entire file, I wanted each row
            to be its own variable (split by comma). Basically, I need all similar POs
            to be grouped together, then pushed out to a temporary holding file for
            further processing.

            --
            Steve Wasser

            the journal of contrarian social discourse and neurotic opinion
            "Hector Martinez" <anonymous@disc ussions.microso ft.com> wrote in message
            news:C9FD7C01-C498-4991-BE4D-AAA1FBD84EC4@mi crosoft.com...[color=blue]
            >
            >
            > I'm sorry, but I can't understand why you need a two-dimensional[/color]
            array....[color=blue]
            >
            > Can you give me a better explanation[/color]


            Comment

            • Steve Wasser

              #7
              Re: Two-Dimension array sort

              Thanks Eric,

              I'll take a shot at that, it'll be a good learning experience. Um, since
              you're a member of the team, why wasn't multidimensiona l array sorting
              included in C#? I'm asking because I'm in the process of converting all our
              ..NET VB code (and switching my core competancy) to C#, and we previously
              shelled out to Perl scripts to handle regular expressions and sorting.
              Sloppy at best to call an external program, so I'm trying to roll it
              internal. Just curious.

              Thanks,

              Steve

              --
              Steve Wasser

              the journal of contrarian social discourse and neurotic opinion
              "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
              news:uLy3ViU2DH A.1660@TK2MSFTN GP09.phx.gbl...[color=blue]
              > One way to do this is to create a class that represents each row of the
              > data, and then have it implement IComparable on the field on which you[/color]
              wish[color=blue]
              > to sort. You can then keep instances of the class into an arraylist, and
              > call sort.
              >
              > If you wanted, that class could store things internally in an arraylist.I
              > would probably go for more descriptive field names.
              >
              > Oh, and call String.Split() to get an array from the comma-delimited[/color]
              fields.[color=blue]
              >
              > --
              > Eric Gunnerson
              >
              > Visit the C# product team at http://www.csharp.net
              > Eric's blog is at http://weblogs.asp.net/ericgu/
              >
              > This posting is provided "AS IS" with no warranties, and confers no[/color]
              rights.[color=blue]
              > "Steve Wasser" <sewasser@hotma il.com> wrote in message
              > news:1005rrobld 38c0f@corp.supe rnews.com...[color=green]
              > > I need to sort a two-dimensional array. Each day I process a file with 9
              > > comma-delimited fields, and varying amount of records (lines). I want to
              > > pull in each line, split it according to the comma into a two[/color][/color]
              dimensional[color=blue][color=green]
              > > array (Perl has it f'r crissake), and sort by one of the fields[/color][/color]
              (Purchase[color=blue][color=green]
              > > Order #). Trouble is, Array.Sort only supports single dimension arrays.
              > > Originally I was thinking of making a jagged array, pulling out the[/color]
              > Purchase[color=green]
              > > Order element, assigning it to a key value corresponding to the single[/color]
              > array[color=green]
              > > line, sort and re-construct the lines, sorted. Easy to say in English,[/color][/color]
              not[color=blue][color=green]
              > > so easy for a C# noob.
              > >
              > > --
              > > Steve Wasser
              > > http://xdissent.com
              > > the journal of contrarian social discourse and neurotic opinion
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Steve Wasser

                #8
                Re: Two-Dimension array sort

                Oh, one more question, is there a significant difference between
                String.Split and Regex.Split? Or just two different means to an end...

                --
                Steve Wasser

                the journal of contrarian social discourse and neurotic opinion
                "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
                news:uLy3ViU2DH A.1660@TK2MSFTN GP09.phx.gbl...[color=blue]
                > One way to do this is to create a class that represents each row of the
                > data, and then have it implement IComparable on the field on which you[/color]
                wish[color=blue]
                > to sort. You can then keep instances of the class into an arraylist, and
                > call sort.
                >
                > If you wanted, that class could store things internally in an arraylist.I
                > would probably go for more descriptive field names.
                >
                > Oh, and call String.Split() to get an array from the comma-delimited[/color]
                fields.[color=blue]
                >
                > --
                > Eric Gunnerson
                >
                > Visit the C# product team at http://www.csharp.net
                > Eric's blog is at http://weblogs.asp.net/ericgu/
                >
                > This posting is provided "AS IS" with no warranties, and confers no[/color]
                rights.[color=blue]
                > "Steve Wasser" <sewasser@hotma il.com> wrote in message
                > news:1005rrobld 38c0f@corp.supe rnews.com...[color=green]
                > > I need to sort a two-dimensional array. Each day I process a file with 9
                > > comma-delimited fields, and varying amount of records (lines). I want to
                > > pull in each line, split it according to the comma into a two[/color][/color]
                dimensional[color=blue][color=green]
                > > array (Perl has it f'r crissake), and sort by one of the fields[/color][/color]
                (Purchase[color=blue][color=green]
                > > Order #). Trouble is, Array.Sort only supports single dimension arrays.
                > > Originally I was thinking of making a jagged array, pulling out the[/color]
                > Purchase[color=green]
                > > Order element, assigning it to a key value corresponding to the single[/color]
                > array[color=green]
                > > line, sort and re-construct the lines, sorted. Easy to say in English,[/color][/color]
                not[color=blue][color=green]
                > > so easy for a C# noob.
                > >
                > > --
                > > Steve Wasser
                > > http://xdissent.com
                > > the journal of contrarian social discourse and neurotic opinion
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Two-Dimension array sort

                  Steve,[color=blue]
                  > Oh, one more question, is there a significant difference between
                  > String.Split and Regex.Split? Or just two different means to an end...[/color]
                  There is a major significant difference between String.Split and
                  Regex.Split!

                  Regex.Split does pattern matching, while String.Split does character
                  matching. If you want Word matching you can use
                  Microsoft.Visua lBasic.Strings. Split.


                  There are three Split functions in .NET:

                  Use Microsoft.Visua lBasic.Strings. Split if you need to split a string based
                  on a specific word (string). It is the Split function from VB6.

                  Use System.String.S plit if you need to split a string based on a collection
                  of specific characters. Each individual character is its own delimiter.

                  Use System.Text.Reg ularExpressions .RegEx.Split to split based
                  on matching patterns.

                  Hope this helps
                  Jay


                  "Steve Wasser" <sewasser@hotma il.com> wrote in message
                  news:10069q7okj ph00f@corp.supe rnews.com...[color=blue]
                  > Oh, one more question, is there a significant difference between
                  > String.Split and Regex.Split? Or just two different means to an end...
                  >
                  > --
                  > Steve Wasser
                  > http://xdissent.com
                  > the journal of contrarian social discourse and neurotic opinion[/color]
                  <<snip>>


                  Comment

                  • Steve Wasser

                    #10
                    Re: Two-Dimension array sort

                    Cool, thanks for clarifying it.

                    Steve

                    --
                    Steve Wasser

                    the journal of contrarian social discourse and neurotic opinion
                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                    news:%23c431XW2 DHA.264@tk2msft ngp13.phx.gbl.. .[color=blue]
                    > Steve,[color=green]
                    > > Oh, one more question, is there a significant difference between
                    > > String.Split and Regex.Split? Or just two different means to an end...[/color]
                    > There is a major significant difference between String.Split and
                    > Regex.Split!
                    >
                    > Regex.Split does pattern matching, while String.Split does character
                    > matching. If you want Word matching you can use
                    > Microsoft.Visua lBasic.Strings. Split.
                    >
                    >
                    > There are three Split functions in .NET:
                    >
                    > Use Microsoft.Visua lBasic.Strings. Split if you need to split a string[/color]
                    based[color=blue]
                    > on a specific word (string). It is the Split function from VB6.
                    >
                    > Use System.String.S plit if you need to split a string based on a[/color]
                    collection[color=blue]
                    > of specific characters. Each individual character is its own delimiter.
                    >
                    > Use System.Text.Reg ularExpressions .RegEx.Split to split based
                    > on matching patterns.
                    >
                    > Hope this helps
                    > Jay
                    >
                    >
                    > "Steve Wasser" <sewasser@hotma il.com> wrote in message
                    > news:10069q7okj ph00f@corp.supe rnews.com...[color=green]
                    > > Oh, one more question, is there a significant difference between
                    > > String.Split and Regex.Split? Or just two different means to an end...
                    > >
                    > > --
                    > > Steve Wasser
                    > > http://xdissent.com
                    > > the journal of contrarian social discourse and neurotic opinion[/color]
                    > <<snip>>
                    >
                    >[/color]


                    Comment

                    Working...