C # sort arrays in ascending and descending order

I had a problem writing a method that returns true if the elements of the array (number) are sorted in order, ascending or descending, and false if they are not in any sorted order. I can return the correct boolean if the array grows, but I do not know how to check also the descending order in the same method. I currently have:

public static bool IsArraySorted(int[] numbers) { for (int i = 1; i < numbers.Length; i++) { if (numbers[i - 1] > numbers[i]) { return false; } } return true; } 

Can anyone offer help on how to check a sorted descending array? Hooray!

+8
sorting arrays c #
source share
5 answers

It should be something like:

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

Note the use of the ascending variable to preserve the "direction" of the array. It is initialized for the first time when two different elements are detected.

Note that if you want, you can even return the "direction" of the array:

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

and inside if (ascending == null)

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

This is a generic version based on IEnumerable<TSource> :

 public static bool IsSorted<TSource>(IEnumerable<TSource> source, out bool isAscending, Comparer<TSource> comparer = null) { isAscending = true; if (comparer == null) { comparer = Comparer<TSource>.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; } 

Note the use of bool first / TSource previous to handle i - 1 (and the fact that the for loop could skip the first element)

+9
source share

Using 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; } 
+3
source share

To test both cases, one cycle is used:

 public static bool IsSorted<T>(IEnumerable<T> items, Comparer<T> comparer = null) { if(items == null) throw new ArgumentNullException("items"); if(!items.Skip(1).Any()) return true; // only one item if (comparer == null) comparer = Comparer<T>.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); } 

using:

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

If you make it an extension method, you can use it with any type:

 bool ordered = new[]{"A", "B", "C"}.IsSorted(); 
+2
source share
 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; } 
+1
source share

Where is my answer? I wrote it about an hour ago:

  public enum SortType { unsorted = 0, ascending = 1, descending = 2 } public static SortType IsArraySorted(int[] numbers) { bool ascSorted = true; bool descSorted = true; List<int> asc = new List<int>(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); } 

Example:

enter image description here

+1
source share

All Articles