简体   繁体   中英

Multi-level sorting on strings

Here is a same of the raw data i have:

Sana Paden,1098,64228,46285,2/15/2011
Ardelle Mahr,1242,85663,33218,3/25/2011
Joel Fountain,1335,10951,50866,5/2/2011
Ashely Vierra,1349,5379,87475,6/9/2011
Amado Loiacono,1406,62789,38490,7/17/2011
Joycelyn Dolezal,1653,14720,13638,8/24/2011
Alyse Braunstein,1657,69455,52871,10/1/2011
Cheri Ravenscroft,1734,55431,58460,11/8/2011

i used a Filestream with a nested Streamwriter to determine first, how many lines are in the file, 2 to create an array of longs that give me the start of every line in the file. Code and out put follows:

using (FileStream fs = new FileStream(@"C:\SourceDatatoedit.csv", FileMode.Open, FileAccess.Read))
{
    fs.Seek(offset, SeekOrigin.Begin);
    StreamReader sr = new StreamReader(fs);
    {
        while (!sr.EndOfStream && fs.CanRead)
        {
            streamsample = sr.ReadLine();
            numoflines++;

        }// end while block
    }//end stream sr block

    long[] dataArray = new long[numoflines];
    fs.Seek(offset, SeekOrigin.Begin);
    StreamReader dr = new StreamReader(fs);
    {
        numoflines = 0;
        streamsample = "";

        while (!dr.EndOfStream && fs.CanRead)
        {
            streamsample = dr.ReadLine();
            //pointers.Add(numoflines.ToString());
            dataArray[numoflines] = offset;
            offset += streamsample.Length - 1;
            numoflines++;
        }// end while

one string contains name, ID, a loan amount, a payment amount and the payment date. i have a method in place to return the remaining amount by subtracting the payment amount from the loan amount and then dividing that by 100 to get the dollar and cents value.

after doing this i want to order my information by Date, name, and then lastly negative amounts first. i understand i could create a loan class then create a list of loan objects and run Linq for Objects query against the set to obtain this but im trying to do this without the use of Linq....any suggestions?

Depending on the context for your code, you can gain many benefits by introducing a custom class / business object. It will help you provide a good separation of concerns in your code, and thus move to more manageable and testable code. You can implement the IComparable interface so that you can invoke a custom Sort on a collection of type List.

I know you mentioned not to use LINQ. However, you could use one line of code for this lines of code here:

 using (FileStream fs = new FileStream(@"C:\SourceDatatoedit.csv", FileMode.Open,       FileAccess.Read))
{
  fs.Seek(offset, SeekOrigin.Begin);
  StreamReader sr = new StreamReader(fs);
  {
    while (!sr.EndOfStream && fs.CanRead)
    {
        streamsample = sr.ReadLine();
        numoflines++;

    }// end while block
  }//end stream sr block
}

To this one line of code like this:

int numoflines = File.ReadLines("SourceDatatoedit.csv").
    Select(line => line.Split(',')).ToList().Count;

Or you could even just get the List like:

var lines = File.ReadLines("SourceDatatoedit.csv").
    Select(line => line.Split(',')).ToList();

And get the number of lines afterward

 numoflines = lines.Count;

And then continue with your code that you have like:

  long[] dataArray = new long[numoflines];
fs.Seek(offset, SeekOrigin.Begin);
StreamReader dr = new StreamReader(fs);
{
    numoflines = 0;
    streamsample = "";

    while (!dr.EndOfStream && fs.CanRead)
    {
        streamsample = dr.ReadLine();
        //pointers.Add(numoflines.ToString());
        dataArray[numoflines] = offset;
        offset += streamsample.Length - 1;
        numoflines++;
    }// end while

Or just use the List obtained above and work with it like creating an IComparable implementation as @sfuqua suggested above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM