C#按升序和降序排序数组

我编写一个方法,如果数组(数字)的元素按排序顺序,升序或降序,则返回true,如果它们不是任何排序顺序,则返回false。 如果数组是升序但我不知道如何在同一方法中检查降序,我可以返回正确的布尔值。 我目前有:

public static bool IsArraySorted(int[] numbers) { for (int i = 1; i  numbers[i]) return false; } return true; } 

任何人都可以提供有关如何检查已排序的降序数组的帮助吗? 干杯!

它应该是这样的:

 public static bool IsArraySorted(int[] numbers) { bool? ascending = null; for (int i = 1; i < numbers.Length; i++) { if (numbers[i - 1] != numbers[i]) { bool ascending2 = numbers[i - 1] < numbers[i]; if (ascending == null) { ascending = ascending2; } else if (ascending.Value != ascending2) { return false; } } } return true; } 

注意使用ascending变量来保存数组的“方向”。 它是在第一次找到两个不同的元素时初始化的。

请注意,如果需要,您甚至可以返回数组的“方向”:

 public static bool IsArraySorted(int[] numbers, out bool isAscending) { isAscending = true; bool? ascending = null; 

if (ascending == null)

 if (ascending == null) { ascending = ascending2; isAscending = ascending2; } 

这是基于IEnumerable的通用版本:

 public static bool IsSorted(IEnumerable source, out bool isAscending, Comparer comparer = null) { isAscending = true; if (comparer == null) { comparer = Comparer.Default; } bool first = true; TSource previous = default(TSource); bool? ascending = null; foreach (TSource current in source) { if (!first) { int cmp = comparer.Compare(previous, current); if (cmp != 0) { bool ascending2 = cmp < 0; if (ascending == null) { ascending = ascending2; isAscending = ascending2; } else if (ascending.Value != ascending2) { return false; } } } first = false; previous = current; } return true; } 

注意使用bool first / TSource previous处理i - 1 (以及for循环能够“跳过”第一个元素的事实)

使用Linq –

 public static bool IsArraySorted(int[] numbers) { var orderedAsc = numbers.OrderBy(a => a); var orderedDes = numbers.OrderByDescending(a => a); bool isSorted = numbers.SequenceEqual(orderedAsc) || numbers.SequenceEqual(orderedDes); return isSorted; } 

这使用一个循环来测试两种情况:

 public static bool IsSorted(IEnumerable items, Comparer comparer = null) { if (items == null) throw new ArgumentNullException("items"); if (!items.Skip(1).Any()) return true; // only one item if (comparer == null) comparer = Comparer.Default; bool ascendingOrder = true; bool descendingOrder = true; T last = items.First(); foreach (T current in items.Skip(1)) { int diff = comparer.Compare(last, current); if (diff > 0) { ascendingOrder = false; } if (diff < 0) { descendingOrder = false; } last = current; if(!ascendingOrder && !descendingOrder) return false; } return (ascendingOrder || descendingOrder); } 

用法:

 int[] ints = { 1, 2, 3, 4, 5, 6 }; bool isOrderedAsc = IsSorted(ints); // true bool isOrderedDesc = IsSorted(ints.Reverse()); //true 

如果你使它成为扩展方法,你可以使用任何类型:

 bool ordered = new[]{"A", "B", "C"}.IsSorted(); 
 public static boolean checkSortedness(final int[] data) { for (int i = 1; i < data.length; i++) { if (data[i-1] > data[i]) { return false; } } return true; } 

我的答案在哪里? 我大约一小时前写的:

 public enum SortType { unsorted = 0, ascending = 1, descending = 2 } public static SortType IsArraySorted(int[] numbers) { bool ascSorted = true; bool descSorted = true; List asc = new List(numbers); asc.Sort(); for (int i = 0; i < asc.Count; i++) { if (numbers[i] != asc[i]) ascSorted = false; if (numbers[asc.Count - 1 - i] != asc[i]) descSorted = false; } return ascSorted ? SortType.ascending : (descSorted? SortType.descending : SortType.unsorted); } 

例:

在此处输入图像描述