Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按非常特定的顺序对字符串列表进行排序_C# - Fatal编程技术网

C# 按非常特定的顺序对字符串列表进行排序

C# 按非常特定的顺序对字符串列表进行排序,c#,C#,这是我的班级: class Example { string GridValue; } 在我的代码中,我使用要排序的列表。 假设此列表具有以下值: { "P", "Q", "X", "H", "J", "L" } 这是调用Sort()后的输出: 如何使用IComparer界面实现这一点 谢谢 很简单,您可以编写一个比较器,将您的特定顺序考虑在内: public class ExampleComparer : IComparer

这是我的班级:

class Example {
    string GridValue;
}
在我的代码中,我使用要排序的列表。 假设此列表具有以下值:

{
    "P", 
    "Q", 
    "X", 
    "H", 
    "J", 
    "L"
}
这是调用Sort()后的输出:

如何使用IComparer界面实现这一点


谢谢

很简单,您可以编写一个比较器,将您的特定顺序考虑在内:

public class ExampleComparer : IComparer<Example>
{
    private const string Order = "XQLHPJ";

    public int Compare(Example a, Example b)
    {
         int indexA = Order.IndexOf(a.GridValue);
         int indexB = Order.IndexOf(b.GridValue);
         if (indexA < indexB) { return -1; }
         else if (indexB < indexA) { return 1; }
         else { return 0; }
    }
} 
公共类示例比较器:IComparer
{
private const string Order=“XQLHPJ”;
公共整数比较(示例a、示例b)
{
int indexA=Order.IndexOf(a.GridValue);
int indexB=Order.IndexOf(b.GridValue);
if(indexA
很简单,您可以编写一个比较器,将您的特定顺序考虑在内:

public class ExampleComparer : IComparer<Example>
{
    private const string Order = "XQLHPJ";

    public int Compare(Example a, Example b)
    {
         int indexA = Order.IndexOf(a.GridValue);
         int indexB = Order.IndexOf(b.GridValue);
         if (indexA < indexB) { return -1; }
         else if (indexB < indexA) { return 1; }
         else { return 0; }
    }
} 
公共类示例比较器:IComparer
{
private const string Order=“XQLHPJ”;
公共整数比较(示例a、示例b)
{
int indexA=Order.IndexOf(a.GridValue);
int indexB=Order.IndexOf(b.GridValue);
if(indexA
如果每个字母都有一个已知的索引
排序器
(对于未知字母,我们可以设置默认索引):

那么使用起来很简单:

var list = new List<Example>();
var result = list.OrderBy(itm => itm.SortOrder);
var list=newlist();
var result=list.OrderBy(itm=>itm.SortOrder);

如果适用的话,不要忘记语言问题,比如

如果每个字母都有一个已知的索引
SortOrder
(对于未知字母,我们可以设置默认索引):

那么使用起来很简单:

var list = new List<Example>();
var result = list.OrderBy(itm => itm.SortOrder);
var list=newlist();
var result=list.OrderBy(itm=>itm.SortOrder);

如果适用的话,不要忘记语言问题,比如

那么,订单是什么???有规则吗?至少规则不清楚,在我们找到规则之前,你应该先谈谈这一点。我们自己都有可能找到这些字母吗?IComparer只要求你可以决定两个值之间的比较方式。您想要的顺序是任意的还是可以创建一个函数来决定P是大于还是小于J?项目列表是固定的吗?基本上,我给您的输出(在sort()之后)是规则。这意味着,“X”的值最高,然后是“Q”,然后是“L”等等。那么顺序是什么???有规则吗?至少规则不清楚,在我们找到规则之前,你应该先谈谈这一点。我们自己都有可能找到这些字母吗?IComparer只要求你可以决定两个值之间的比较方式。您想要的顺序是任意的还是可以创建一个函数来决定P是大于还是小于J?项目列表是固定的吗?基本上,我给您的输出(在sort()之后)是规则。这意味着,“X”的值最高,然后是“Q”,然后是“L”等等。如果我想这样做,如果其中有一些数字可以按数字顺序排序(优先考虑字母)?@fobia:那么只需将“0123456789”添加到
顺序
常量中。好吧,但这不是最好的方法,我想如果我有一个从1到5000的随机值,对吗?啊哈,你是说不是按数字排序,而是按整数排序?然后,您必须解析比较方法中的输入,确定它是数字还是字母数字等。关键是,只要您正确定义了规则,就可以在比较方法中使用任何类型的比较逻辑。@McFixit Done!谢谢。如果我想这样做,如果其中有一些数字可以按数字顺序排序(优先考虑字母)?@fobia:那么只需在
顺序
常量中加上“0123456789”。好吧,但这不是最好的方法,我想如果我有从1到5000的随机值,对吗?啊哈,你的意思是不按数字排序,但按整数计算?然后,您必须解析比较方法中的输入,确定它是数字还是字母数字等。关键是,只要您正确定义了规则,就可以在比较方法中使用任何类型的比较逻辑。@McFixit Done!谢谢