开发者

sort datetime range

Given the list of ranges datetime (start 开发者_运维技巧and end time), how can I sort them by duration (eg.largest to smallest duration)?


Assuming each object in the list is of the type DateTime then you can use the Ticks property to sort them.

http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx

If they are ranges, you can subtract the Ticks of the start time from the Ticks of the end time and use the resulting value to sort.

For example:

DateTime Start = YourStartValue;
DateTime End = YourEndValue;
long Range = End.Ticks - Start.Ticks;

This will give you a number of "Ticks" that represent the length of time in your range, the smaller the number, the shorter the duration.

Repeat this for each range, and then you can sort the results.


You need to save the duration in third property then apply sort on third property.

If you are populating the list from database and hold it in a DataTable. Then you can add an Expression based Column then apply sort on the datatable on the expression based column.

If you have a generic list of class's object. Then your property should look similar like this.

public long Duration {
    get {
        return (endDate - startDate).Ticks;
    }
}

I guess you have start and end both are of DataTime type.


Assuming A en B are DateTime, you can use a TimeSpan like this

long sort = (A - B).Ticks;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜