He does not exist. But this is easy to do: create a list with a merged version of the list and the same list that you want to sort, and make sure that both elements of the combined list are the same.
Something like that:
import org.junit.Assert._ val sortedList = List(1, 3, 5, 7) val unsortedList = List(10, 1, 8, 3, 5, 5, 2, 9) // detailed test. It passes. sortedList .zip(sortedList.sortWith((a,b) => a.compareTo(b) < 0)) // this is the required sorting criteria. .foreach(x => assertEquals("collection is not sorted", x._1, x._2)) // easier to read but similar test. It fails. unsortedList .zip(unsortedList.sorted) // this is the required sorting criteria. .foreach(x => assertEquals("collection is not sorted", x._1, x._2))
may be a function:
def isSorted(list: List[Int]): Boolean = !list.zip(list.sortWith((a, b) => a.compareTo(b) < 0)).exists(p => !p._1.equals(p._2))
source share