Solved

c# - Sorting an string arraylist by a comma delimited field containing date and a time

Posted on 2009-07-15
4
532 Views
Last Modified: 2012-05-07
Hi everyone,

I'd appreciate some help with sorting a string arrayList by date and time which is contained in the array element, but in a comma delimited field... for example...

"2346592483, 2008-10-01 19:17:13, buy, open"
"2346592483, 2008-10-01 07:11:01, sell, open"

Therefore a you can see I'd ike to sort the arraylist so that it is cronological by date AND time.

Let me please thank you in advance for your gracious help.

Cheers - Dynotoe
0
Comment
Question by:Dynotoe
4 Comments
 
LVL 8

Accepted Solution

by:
RyanAndres earned 500 total points
ID: 24863997
I used to do a lot of data processing of log files and the way I did it all the time was to create objects that represent a record in the file.

Assuming you have a standard format for each line in the file, you should really break it up first then handle the objects accordingly.

For example, create an object that represents this line (I'll assume the field names)
"2346592483, 2008-10-01 19:17:13, buy, open"

So what you would do is iterate through each line in the file, load it into a Record object and add it to a List<Record>.

From here you could simply use linq to sort ie:
(Freehanding this)
// originalList contains all your lines loaded into objects
List<Record> sortedList = originalList.OrderBy(r => r.DateTime).ToList();

^ This one line will sort your entire collection by the date time. Change the property to RecordId and it sorts by RecordId. Simple huh? :D
class Record

{

    public int RecordId{ get; private set; }

    public DateTime LogTime { get; private set; }

    public string Type { get; private set; }

    public string Status { get; private set; }
 

    public void Load(string record)

    {

        // Record = "2346592483, 2008-10-01 19:17:13, buy, open"

        string[] parts = record.Split(',');

        RecordId = parts[0];

        LogTime = DateTime.Parse(parts[1]);

        Type = parts[2];

        Status = parts[3];

    }

}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 24864013
You might consider implementing your own custom object implementing IComparable which would perform comparisons based on the date/time field, then you can use Array.Sort.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24864557
What version C# you working in?...
0
 

Author Comment

by:Dynotoe
ID: 24873224
I just ended up convrting the date string to be the first field in 20080101 format and used the sort() fucntion of the arrayList.

Thanks for all your help!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now