如何按工作日顺序(如日历周)而不是按字母顺序(在 C# 中)排序?

发布于 2024-10-08 05:01:40 字数 328 浏览 2 评论 0原文

我无法弄清楚如何按日期对独立存储中的 XML 文件中的查询输出进行排序,该日期是 xml 文件中的值。

我的意思是它将按当天的第一个字母排序,因此它将返回星期五作为第一个字母(因为其中有“F”)。但这不是我想要的,相反,它们应该按工作日的顺序排序,即星期一、星期二、星期三、星期四、星期五。

有什么方法可以转换包含文本中的日期的字符串,例如。 “星期一”到日期时间来排序?

我正在使用的代码是这样的:

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

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.

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-10-15 05:01:40

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

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 

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 
生生漫 2024-10-15 05:01:40

这允许您按一周中的任意一天进行排序:

public class DayOfWeekComparer : IComparer<DayOfWeek>
{
    public static int Rank(DayOfWeek firstDayOfWeek, DayOfWeek x)
    {
        return (int)x + (x < firstDayOfWeek ? 7 : 0);
    }

    public static int Compare(DayOfWeek firstDayOfWeek, DayOfWeek x, DayOfWeek y)
    {
        return Rank(firstDayOfWeek, x).CompareTo(Rank(firstDayOfWeek, y));
    }

    DayOfWeek firstDayOfWeek;

    public DayOfWeekComparer(DayOfWeek firstDayOfWeek)
    {
        this.firstDayOfWeek = firstDayOfWeek;
    }

    public int Compare(DayOfWeek x, DayOfWeek y)
    {
        return DayOfWeekComparer.Compare(this.firstDayOfWeek, x, y);
    }
}

This allows you to sort by an arbitrary day of the week:

public class DayOfWeekComparer : IComparer<DayOfWeek>
{
    public static int Rank(DayOfWeek firstDayOfWeek, DayOfWeek x)
    {
        return (int)x + (x < firstDayOfWeek ? 7 : 0);
    }

    public static int Compare(DayOfWeek firstDayOfWeek, DayOfWeek x, DayOfWeek y)
    {
        return Rank(firstDayOfWeek, x).CompareTo(Rank(firstDayOfWeek, y));
    }

    DayOfWeek firstDayOfWeek;

    public DayOfWeekComparer(DayOfWeek firstDayOfWeek)
    {
        this.firstDayOfWeek = firstDayOfWeek;
    }

    public int Compare(DayOfWeek x, DayOfWeek y)
    {
        return DayOfWeekComparer.Compare(this.firstDayOfWeek, x, y);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文