Solved

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

Posted on 2009-07-15
4
539 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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