Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 124
Achieve json result 2 65
Programmatically signing Word macros 4 61
Angular - break the dataset in chunks? 7 40
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

28 Experts available now in Live!

Get 1:1 Help Now