如何按工作日的顺序(如日历周)而不是字母顺序(在 C# 中)排序? [英] How to sort by order of the weekdays (as in a calendar week) instead of alphabetical order (in C#)?

查看:33
本文介绍了如何按工作日的顺序(如日历周)而不是字母顺序(在 C# 中)排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何按日期对独立存储中的 XML 文件中的查询的输出进行排序,该日期是 xml 文件中的一个值.

I cannot work out how to sort the output from a query in an XML file in isolated storage by the day which is a value in the xml file.

我的意思是它将按当天的第一个字母排序,因此它将作为第一个返回星期五(因为其中包含F").但这不是我想要的,而是应该按工作日的顺序排序,即星期一、星期二、星期三、星期四、星期五.

By this I mean it will sort by the first letter(s) of the day, so it will return Friday as the first one (because of the "F" in it). But that is not what I want, instead they should be sorted by the order of the weekdays, i.e. monday, tuesday, wednesday, thursday, friday.

有什么方法可以转换包含文本中的日期的字符串,例如.星期一"到日期时间进行排序?

Is there any way I can convert a string which contains the day in text eg. "monday" to a DateTime to sort by?

我使用的代码是这样的:

The code I am using is like so:

 let obv = (string)query.Element("day")
 orderby obv 
 select new obv 

推荐答案

您可以按值在 CultureInfo.CurrentCulture.DateFormat.DayNames 数组中的索引进行排序:

You can sort by the value's index in the CultureInfo.CurrentCulture.DateFormat.DayNames array:

var daynames = Array.ConvertAll(
    CultureInfo.CurrentCulture.DateFormat.DayNames,
    d => d.ToUpperInvariant()
);

from ...
let obv = (string)query.Element("day")
orderby Array.IndexOf(daynames, obv.ToUpperInvariant())
select new obv 

这篇关于如何按工作日的顺序(如日历周)而不是字母顺序(在 C# 中)排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆