简体   繁体   English

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

[英]How to sort by order of the weekdays (as in a calendar week) instead of alphabetical order (in C#)?

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. 我无法弄清楚如何在隔离存储中的XML文件中查询输出到xml文件中的值。

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). 我的意思是它将按当天的第一个字母排序,因此它将作为第一个字母返回星期五(因为它中的“F”)。 But that is not what I want, instead they should be sorted by the order of the weekdays, ie 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? “星期一”到DateTime排序?

The code I am using is like so: 我使用的代码是这样的:

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

You can sort by the value's index in the CultureInfo.CurrentCulture.DateFormat.DayNames array: 您可以按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 

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何将数据网格中的列按字母顺序排序? - C# How do I sort the columns in a datagrid into alphabetical order? 如何使用c#按字母顺序对文本文件进行排序 - How to sort a text file in alphabetical order using c# 按工作日的顺序而不是按字母顺序合并星期几 - Merge sort days of week in order of weekday and not alphabetical 如何根据其中一个按字母顺序对 4 个单独的字符串数组(具有相同的长度)进行排序? (在 C# 中) - How can I sort 4 seperate string arrays (with the same lenght), according to one of them in alphabetical order? (in C#) C#如何检查项目列表是否按字母顺序排列 - c# how to check if list of Items are in Alphabetical order or not 如何在C#中按字母顺序打印字符串中出现的字符? - How to print occurrence of a character in string in alphabetical order in C#? 在C#中按字母顺序生成文件 - Generating files in alphabetical order in C# 按字母顺序显示foreach列表c# - Displaying a foreach list in alphabetical order c# 在C#中,我如何基于第二列而不是仅按字母顺序的值以升序对锯齿状和二维矩形数组进行排序? - In C# how could I sort a jagged and 2d rectangular array based on 2nd column in ascending order, based on values not just alphabetical order? 如何快速组织源代码(c#)中的函数按字母顺序排序? - How to quickly organize functions in source code (c#) to order by alphabetical order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM