From 7aef088f7bee1b2921ed9c0492385e6d5552546c Mon Sep 17 00:00:00 2001 From: James Strachan Date: Mon, 26 Mar 2012 22:30:28 +0100 Subject: [PATCH] added a test of list sorting using a hand crafted Comparator; though using the comparator function would be nice when KT-729 is fixed --- libraries/stdlib/test/CompareTest.kt | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/test/CompareTest.kt b/libraries/stdlib/test/CompareTest.kt index 78c9239bbde..81235609264 100644 --- a/libraries/stdlib/test/CompareTest.kt +++ b/libraries/stdlib/test/CompareTest.kt @@ -1,9 +1,11 @@ package test.compare +import java.util.Comparator import kotlin.test.* import org.junit.Test class Item(val name: String, val rating: Int) { + fun toString() = "Item($name, $rating)" } class CompareTest { @@ -25,7 +27,7 @@ class CompareTest { assertTrue(diff == 0) } - Test fun createComparator() { + Test fun sortUsingComparatorHelperMethod() { val c = comparator({it.rating}, {it.name}) println("Created comparator $c") @@ -38,4 +40,23 @@ class CompareTest { println("Sorted list in rating order $items") } } + + Test fun sortUsingCustomComparator() { + val c = object : Comparator{ + override fun compare(o1: Item?, o2: Item?): Int { + return compareBy(o1, o2, {(it: Item) -> it.name}, {(it: Item) -> it.rating}) + } + override fun equals(obj: Any?): Boolean { + return this == obj + } + } + println("Created comparator $c") + + val diff = c.compare(v1, v2) + assertTrue(diff > 0) + val items = arrayList(v1, v2) + items.sort(c) + println("Sorted list in rating order $items") + } + } \ No newline at end of file