Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code module that exposes the extract file data prior to being written to the extract file. Each member of the input array represents a line of text that will normally be written to the extract file.
The extract file is in CSV format with fields (BankID, DDA, DepositDate,TotalAmount,TotalChecks). I don't have access to backend SQL nor the sln. How can I sort by BankID before sending it to extract file?

Consider the data being dumped in this format:
VB
BankID ,               DDA, DepositDate,  TotalAmount, TotalChecks
    224152,        5800956996,  11/04/2013,   $15,969.32,          10
    4522424,        5800956996,  11/04/2013,   $26,303.26,          21
    415224,        5800956996,  11/04/2013,        $0.00,           0
    985758,        5800956996,  11/04/2013,   $91,172.66,           8
    11124,        5800956996,  11/04/2013,   $37,395.50,           7


XML
public class PostProcessing {

        public object ProcessExtract(List<string> FileRows) {

            List<string> arRetVal = new List<string>();

            // Loop through each row of text of the extract file and modify
            // as necessary
            foreach(string rowText in FileRows) {
                arRetVal.Add(rowText);
            }

            return(arRetVal);
        }
Posted

Please see: http://msdn.microsoft.com/en-us/library/3da4abas%28v=vs.110%29.aspx[^].

Alternatively, you might need some other class which is kept sorted as you add values. Usually, this is useful if you have one "main" sort criteria, not sorting by different criteria on different events. Please see:
http://msdn.microsoft.com/en-us/library/ms132319(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c(v=vs.110).aspx[^].

Also, if you are using some relational database, it would be more correct to sort data by your query instead, to get an already sorted data set. The sorting is specified using the SORT BY clause. See, for example:
http://www.techonthenet.com/sql/order_by.php[^].

—SA
 
Share this answer
 
v3
Comments
Maciej Los 13-Oct-14 17:17pm    
5ed!
Sergey Alexandrovich Kryukov 13-Oct-14 17:19pm    
Thank you, Maciej.
—SA
How about some Linq voodoo ?

Assumptions:

1. BankID is always a unique number

2. the desired result order is by BankID numeric ascending order
C#
private List<string> ProcessCSV(string csv)
{
    // clean up
    csv = csv.Replace(" ", String.Empty);

    IEnumerable<string> lines = csv.Split(new string[] {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);

    // skip the first line
    return lines.Skip(1).ToDictionary
    (
        itm => itm.Substring(0, itm.IndexOf(','))
    )
    .OrderBy(kvp => Convert.ToInt32(kvp.Key))
    .Select(kvp => kvp.Value).ToList();
}

private string csvTrial = @"BankID ,               DDA, DepositDate,  TotalAmount, TotalChecks
224152,        5800956996,  11/04/2013,   $15,969.32,          10
4522424,        5800956996,  11/04/2013,   $26,303.26,          21
415224,        5800956996,  11/04/2013,        $4.12,           0
985758,        5800956996,  11/04/2013,   $91,172.66,           8
11124,        5800956996,  11/04/2013,   $37,395.50,           7";
Test:
C#
List<string> accounts = ProcessCSV(csvTrial);
Result:
11124,5800956996,11/04/2013,$37,395.50,7
224152,5800956996,11/04/2013,$15,969.32,10
415224,5800956996,11/04/2013,$4.12,0
985758,5800956996,11/04/2013,$91,172.66,8
4522424,5800956996,11/04/2013,$26,303.26,21
 
Share this answer
 
v2
Comments
Bhis 14-Oct-14 10:41am    
Thank you @BillWoodruff. It worked the way I wanted to. Just out of curiosity, can this be achieved without using Linq?
BillWoodruff 14-Oct-14 11:05am    
Yes, you could achieve this without using Linq. I suspect, but am not sure, you could use RegEx's as part of the solution. To summarize this:

1. get a collection of the lines

2. get a collection of the first entry in each line converted to integer

3. sort the collection of integers

4. somehow re-order the lines based on the sorted collection of integers

I'd rather do it with Linq, but who knows what other solutions may be possible :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900