By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,193 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,193 IT Pros & Developers. It's quick & easy.

Two-Dimension array sort

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

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

Can you give me a better explanation
Nov 15 '05 #2

P: n/a

"Steve Wasser" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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.

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
Nov 15 '05 #3

P: n/a
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" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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

Nov 15 '05 #4

P: n/a

I think what you say is his more elegant option...
Nov 15 '05 #5

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...


I'm sorry, but I can't understand why you need a two-dimensional array....
Can you give me a better explanation

Nov 15 '05 #6

P: n/a
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]" <er****@online.microsoft.com> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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


Nov 15 '05 #7

P: n/a
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]" <er****@online.microsoft.com> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...
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" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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


Nov 15 '05 #8

P: n/a
Steve,
Oh, one more question, is there a significant difference between
String.Split and Regex.Split? Or just two different means to an end... 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" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com... 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

<<snip>>
Nov 15 '05 #9

P: n/a
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]" <Ja************@msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Steve,
Oh, one more question, is there a significant difference between
String.Split and Regex.Split? Or just two different means to an end... 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" <se******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
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

<<snip>>

Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.