Two-Dimension array sort

S

Steve Wasser

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.
 
G

Guest

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

Can you give me a better explanation
 
D

David Browne

Steve Wasser said:
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
 
E

Eric Gunnerson [MS]

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.
 
S

Steve Wasser

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.
 
S

Steve Wasser

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
 
S

Steve Wasser

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

Jay B. Harlow [MVP - Outlook]

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 said:
Oh, one more question, is there a significant difference between
String.Split and Regex.Split? Or just two different means to an end...
<<snip>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top