Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-07-15
4
541 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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