Solved

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

Posted on 2009-07-15
4
543 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
[X]
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
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 86

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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