Reputation: 1498
I am using asp.net 2005 on my system and creating a application. I have an issue with date format sorting. I have a table including three fields like "user name, date time, visit page".
My records for this as below
user name date time visited page
xyz 4/4/2013 5:30:45 PM aaa.aspx
xyz 4/4/2013 6:35:15 PM aaa.aspx
xyz 4/4/2013 7:55:45 PM aaa.aspx
xyz 4/4/2013 10:05:45 PM aaa.aspx
xyz 4/4/2013 11:06:45 PM aaa.aspx
I want to sorted this without using java script or jquery. and the output should be
user name date time visited page
xyz 4/4/2013 11:06:45 PM aaa.aspx
xyz 4/4/2013 10:05:45 PM aaa.aspx
xyz 4/4/2013 7:55:45 PM aaa.aspx
xyz 4/4/2013 6:35:15 PM aaa.aspx
xyz 4/4/2013 5:30:45 PM aaa.aspx
But I got the output like this
user name date time visited page
xyz 4/4/2013 7:55:45 PM aaa.aspx
xyz 4/4/2013 6:35:15 PM aaa.aspx
xyz 4/4/2013 5:30:45 PM aaa.aspx
xyz 4/4/2013 11:06:45 PM aaa.aspx
xyz 4/4/2013 10:05:45 PM aaa.aspx
I tried to sorted this via dataview but its not working properly can any one help me out for this issue.
Upvotes: 0
Views: 386
Reputation: 241525
There are many other ways to read a CSV, including just as a raw text file and simple string parsing. You can use some of the suggestions in the other answers here, or you can just populate the results into a DataTable, or whatever. Use whatever you are comfortable with.
The important bit that hasn't been mentioned in the other answers is that you need to be explicit about the datetime format that you are reading. For example, how do I know that 1/2/2013
represents either January 2nd, or February 1st?
Since the file you have is not using an invariant date format (like ISO8601 for example), then you should somewhere be explicitly providing a format, such as:
DateTime dt = DateTime.ParseExact(YourDateString,
"M/d/yyyy", // or "d/M/yyyy"
CultureInfo.InvariantCulture);
Upvotes: 1
Reputation: 43077
Read your .csv values into an object with a strongly typed DateTime property and then sort.
CsvValue[] values = ReadCsvFile().OrderBy(value => value.DateTime);
Where CsvValue is
public class CsvValue
{
public string UserName {get; set;}
public DateTime DateTime {get; set;}
public string VisitedPage {get; set;}
}
Upvotes: 1
Reputation: 12243
Set your CSV as a ODBC source. Then you have to use Schema.ini and set that field as a datetime. See the link: http://msdn.microsoft.com/en-us/library/ms709353.aspx
It should explain this. Then once you're done you can do a SQL Query, and do a simple
SELECT * ORDER BY [date time] desc
I personally hate using flat text files, if you could use DTS and get it into a relational database of some sort, that'd be a good start.
Upvotes: 1