1,105,169 Community Members

Sorting and Subtotaling

Member Avatar
spowel4
Junior Poster in Training
72 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Given a comma-delimited file like this example:

"92528","1","36","38280","51801","PLANT 5 - LINE 1","37",
"92528","1","8","37450","51801","PLANT 5 - LINE 1","37",
"92528","2","8","37896","51801","PLANT 5 - LINE 1","37",
"92528","3","8","37450","51801","PLANT 5 - LINE 1","37",
"92528","61","32","37450","51802","PLANT 5 - LINE 2","37",
"92528","62","16","38721","51802","PLANT 5 - LINE 2","37",
"92528","63","8","38722","51802","PLANT 5 - LINE 2","37",
"92528","64","4","37450","51802","PLANT 5 - LINE 2","37",
"92528","149","8","37450","51804","PLANT 5 - LINE 4","37",
"92528","150","2","38668","51804","PLANT 5 - LINE 4","37",
"92528","151","8","37450","51804","PLANT 5 - LINE 4","37",
"92528","152","2","38668","51804","PLANT 5 - LINE 4","37",

I need to sort it first by the 6th field of each line ("PLANT 5 - LINE 1" on the first line),
then by the 4th field of each line ("38280" on the first line); then I need to get a subtotal for each item in the 4th field so that I only end up with 1 line per item within each plant.
I need to end up with this:

"PLANT 5 - LINE 1","37450","16"
"PLANT 5 - LINE 1","37896","8"
"PLANT 5 - LINE 1","38280","36"
"PLANT 5 - LINE 2","37450","36"
"PLANT 5 - LINE 2","38721","16"
"PLANT 5 - LINE 2","38722","8"
"PLANT 5 - LINE 4","38668","4"
"PLANT 5 - LINE 4","37450","16"

Not sure where to start on this, can someone offer suggestions?

Member Avatar
AleMonteiro
Master Poster
758 posts since Aug 2010
Reputation Points: 115 [?]
Q&As Helped to Solve: 143 [?]
Skill Endorsements: 29 [?]
 
0
 

I'd start parsing the text file data into a object model and keep it in an list, like List<Product>.

Then I'd use the generic sorting functionalities for performing the sort, this you can see at http://www.codeproject.com/Tips/323985/Sorting-using-Csharp-Lists

Member Avatar
ddanbe
Posting Expert
5,340 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 906 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

Or you could drop your data in a DataGridView and use some of the many properties and methods of this rather big, but very usefull object. See this example.

Member Avatar
spowel4
Junior Poster in Training
72 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here's what I've done so far, and it appears to be sorting correctly:

            DataTable dtResult;

            using (GenericParsing.GenericParserAdapter p = new GenericParsing.GenericParserAdapter(@"s:\hp\haylin.csv"))
            {
                dtResult = p.GetDataTable();
            }

            DataView dv = dtResult.DefaultView;
            dv.Sort = "Column6, Column4 ASC";
            DataTable dt2 = dv.ToTable("Table2");

Can you offer some suggestions on how to do the next step, which is to get subtotals per item?

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: