473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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
Nov 15 '05 #1
9 22025

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

"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
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

I think what you say is his more elegant option...
Nov 15 '05 #5
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.