473,761 Members | 9,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22062

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******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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.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
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******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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,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
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Hector Martinez" <an*******@disc ussions.microso ft.com> wrote in message
news:C9******** *************** ***********@mic rosoft.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 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
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
"Eric Gunnerson [MS]" <er****@online. microsoft.com> wrote in message
news:uL******** ******@TK2MSFTN GP09.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******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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******** ******@TK2MSFTN GP09.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******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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.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" <se******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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******** *******@tk2msft ngp13.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.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" <se******@hotma il.com> wrote in message
news:10******** *****@corp.supe rnews.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
1786
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. Inside (or just after) the same query that searches for available seats, I need to SIMULTANEOUSLY mark those seats as "on hold". I've only read about, but not yet used MySQL transactions, and wonder if this simultaneous "search-and-hold"...
8
1748
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). TABLE matchResults cont_id team_id contest_result 1 1 3 1 2 5
6
1883
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
9385
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 record in the second table and call the update method of the data adapter the command builders update command text is for the first table. Can the command builder handle two tables? Code example: Dim oCOnn As New SqlConnection("Data Source=.;" &...
6
4082
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 the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
7
12895
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 compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function or simple code snippet.. Thank U -- Peter...
0
1698
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 became realllllllllllly slow. Content in LoginView Role Groups was not displaying even after a user in a role had logged in. It was taking about 15 seconds or so for the login control to display when the login link was selected on the homepage....
9
5273
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 w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
9
2022
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 million times, but I can't seem to find the answer. For example, I have two classes stored in separate files as such. File: one.py ======== class One:
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.