我有一个字符串列表,它是用特定方法排序的:
list1 : { "E", "C", "B", "A"};
现在我想检查其他数组,如果它是根据 list1 排序的,那么它是正确的。
例如 :
array: { "A", "C", "E", "B" } //false - not ordered according to list1
array: { "E", "B", "C" } //false - not ordered according to list1
array: { "C", "A"} //true- ordered according to list1
功能代码:
List<string> list1 = new List<string> { "E", "C", "B", "A"};
public static bool IsSorted(string[] array)
{
for (int i = 0; i < array.Length; i++)
{
if () //Your opinion
{
return false;
}
}
return true;
}
我认为使用列表而不是数组很容易
这是一个例子:
List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);
list1.Add(3);
List<int> list2 = new List<int>();
list2.Add(2);
list2.Add(3);
Console.WriteLine( list1.Intersect(list2).SequenceEqual(list2)); // Will return true
根据您的示例发布了一个关于 int 变量的示例,您可以使用您想要的任何类型
更新:它也适用于数组(感谢@Matthew Watson)
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 2, 3 };
Console.WriteLine( arr1.Intersect(arr2).SequenceEqual(arr2)); // will return true
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句