We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,382 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sorting and Subtotaling

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?

4
Contributors
4
Replies
9 Hours
Discussion Span
3 Months Ago
Last Updated
5
Views
spowel4
Junior Poster in Training
54 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

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.

ddanbe
Industrious Poster
4,298 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 723
Skill Endorsements: 26

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?

spowel4
Junior Poster in Training
54 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0841 seconds using 2.66MB