Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a list is ordered?

I am doing some unit tests and I want to know if there's any way to test if a list is ordered by a property of the objects it contains.

Right now I am doing it this way but I don't like it, I want a better way. Can somebody help me please?

// (fill the list) List<StudyFeedItem> studyFeeds =      Feeds.GetStudyFeeds(2120, DateTime.Today.AddDays(-200), 20);     StudyFeedItem previous = studyFeeds.First();  foreach (StudyFeedItem item in studyFeeds) {     if (item != previous)     {         Assert.IsTrue(previous.Date > item.Date);     }      previous = item; } 
like image 590
Nicole Avatar asked Dec 21 '09 13:12

Nicole


People also ask

How do I check if a Python list is ordered?

The simplest way to check this is run a loop for first element and check if we could find any smaller element than it after that element, if yes, the list is not sorted. if ( not flag) : print ( "Yes, List is sorted." )

How do I check if a list is in ascending order?

You can go through the example given: # using naive method to # check sorted list flag = 0 i = 1 while i < len(test_list): if(test_list[i] < test_list[i - 1]): flag = 1 i += 1 # printing result if (not flag) : print ("Yes, List is sorted.") else : print ("No, List is not sorted.")

How do you check if a list is sorted or not in Java?

4.1.reverseOrder() to check if a list is sorted in reverse order. In addition, we can use natural(). nullFirst() and natural(). nullLast() to check if null appears to the first or the last of the sorted list.

How check list is sorted or not in C#?

GetItems(true) returns an ordered list of items, then test that. But don't test that items. OrderBy(x => x, new YourComparer()) does indeed sort the list. However, do unit test that YourComparer does indeed compare correctly.


2 Answers

If you are using MSTest, you may want to take a look at CollectionAssert.AreEqual.

Enumerable.SequenceEqual may be another useful API to use in an assertion.

In both cases you should prepare a list that holds the expected list in the expected order, and then compare that list to the result.

Here's an example:

var studyFeeds = Feeds.GetStudyFeeds(2120, DateTime.Today.AddDays(-200), 20);    var expectedList = studyFeeds.OrderByDescending(x => x.Date); Assert.IsTrue(expectedList.SequenceEqual(studyFeeds)); 
like image 156
Mark Seemann Avatar answered Sep 23 '22 10:09

Mark Seemann


A .NET 4.0 way would be to use the Enumerable.Zip method to zip the list with itself offset by one, which pairs each item with the subsequent item in the list. You can then check that the condition holds true for each pair, e.g.

var ordered = studyFeeds.Zip(studyFeeds.Skip(1), (a, b) => new { a, b })                         .All(p => p.a.Date < p.b.Date); 

If you're on an earlier version of the framework you can write your own Zip method without too much trouble, something like the following (argument validation and disposal of the enumerators if applicable is left to the reader):

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(     this IEnumerable<TFirst> first,     IEnumerable<TSecond> second,     Func<TFirst, TSecond, TResult> selector) {     var e1 = first.GetEnumerator();     var e2 = second.GetEnumerator();     while (e1.MoveNext() & e2.MoveNext()) // one & is important         yield return selector(e1.Current, e2.Current); } 
like image 24
Greg Beech Avatar answered Sep 20 '22 10:09

Greg Beech