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 http://xdissent.com
the journal of contrarian social discourse and neurotic opinion | | | | 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 | | | | re: Two-Dimension array sort
"Steve Wasser" <sewasser@hotmail.com> wrote in message
news:1005rrobld38c0f@corp.supernews.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.Collections.IComparer
{
int ix;
public ArrayComparer(int SortFieldIndex)
{
ix = SortFieldIndex;
}
public int Compare(object x, object y)
{
IComparable cx = (IComparable)((Array)x).GetValue(ix);
IComparable cy = (IComparable)((Array)y).GetValue(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.WriteLine(line[0]);
}
System.Array.Sort(lines,new ArrayComparer(2));
foreach (string[] line in lines)
{
Console.WriteLine(line[0]);
}
}
David | | | | 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@hotmail.com> wrote in message
news:1005rrobld38c0f@corp.supernews.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] | | | | re: Two-Dimension array sort
I think what you say is his more elegant option... | | | | 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,NSB,5000
12/30/2003,564,86827,4212672,27082,SX1,1500
01/01/2004,396,88513,4220205,32262,MM6,1500
01/01/2004,396,88513,4220205,32262,NM6,7000
01/02/2004,302,88513,4216938,22837,MM6,1200
01/02/2004,302,88513,4216938,22837,NM6,5500
01/02/2004,302,88513,4216938,22837,PM6,2000
I need to sort according to the 4th field. I was using StreamReader.ReadLine
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 http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Hector Martinez" <anonymous@discussions.microsoft.com> wrote in message
news:C9FD7C01-C498-4991-BE4D-AAA1FBD84EC4@microsoft.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] | | | | 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 multidimensional 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 http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Eric Gunnerson [MS]" <ericgu@online.microsoft.com> wrote in message
news:uLy3ViU2DHA.1660@TK2MSFTNGP09.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@hotmail.com> wrote in message
> news:1005rrobld38c0f@corp.supernews.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] | | | | 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 http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Eric Gunnerson [MS]" <ericgu@online.microsoft.com> wrote in message
news:uLy3ViU2DHA.1660@TK2MSFTNGP09.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@hotmail.com> wrote in message
> news:1005rrobld38c0f@corp.supernews.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] | | | | 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.VisualBasic.Strings.Split.
There are three Split functions in .NET:
Use Microsoft.VisualBasic.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.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.
Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.
Hope this helps
Jay
"Steve Wasser" <sewasser@hotmail.com> wrote in message
news:10069q7okjph00f@corp.supernews.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>> | | | | re: Two-Dimension array sort
Cool, thanks for clarifying it.
Steve
--
Steve Wasser http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:%23c431XW2DHA.264@tk2msftngp13.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.VisualBasic.Strings.Split.
>
>
> There are three Split functions in .NET:
>
> Use Microsoft.VisualBasic.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.Split 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.RegularExpressions.RegEx.Split to split based
> on matching patterns.
>
> Hope this helps
> Jay
>
>
> "Steve Wasser" <sewasser@hotmail.com> wrote in message
> news:10069q7okjph00f@corp.supernews.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] |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|