| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * The signature of a generic comparison function. | |
| 9 * | |
| 10 * A comparison function represents an ordering on a type of objects. | |
| 11 * A total ordering on a type means that for two values, either they | |
| 12 * are equal or one is greater than the other (and the latter must then be | |
| 13 * smaller than the former). | |
| 14 * | |
| 15 * A [Comparator] function represents such a total ordering by returning | |
| 16 * | |
| 17 * * a negative integer if [a] is smaller than [b], | |
| 18 * * zero if [a] is equal to [b], and | |
| 19 * * a positive integer if [a] is greater than [b]. | |
| 20 */ | |
| 21 typedef int Comparator<T>(T a, T b); | |
| 22 | |
| 23 /** | |
| 24 * Interface used by types that have an intrinsic ordering. | |
| 25 * | |
| 26 * The [compareTo] operation defines a total ordering of objects, | |
| 27 * which can be used for ordering and sorting. | |
| 28 * | |
| 29 * The [Comparable] interface should be used for the natural ordering of a type. | |
| 30 * If a type can be ordered in more than one way, | |
| 31 * and none of them is the obvious natural ordering, | |
| 32 * then it might be better not to use the [Comparable] interface, | |
| 33 * and to provide separate [Comparator]s instead. | |
| 34 * | |
| 35 * It is recommended that the order of a [Comparable] agrees | |
| 36 * with its operator [==] equality (`a.compareTo(b) == 0` iff `a == b`), | |
| 37 * but this is not a requirement. | |
| 38 * For example, [double] and [DateTime] have `compareTo` methods | |
| 39 * that do not agree with operator [==]. | |
| 40 * For doubles the [compareTo] method is more precise than the equality, | |
| 41 * and for [DateTime] it is less precise. | |
| 42 * | |
| 43 * Examples: | |
| 44 * | |
| 45 * (0.0).compareTo(-0.0); // => 1 | |
| 46 * 0.0 == -0.0; // => true | |
| 47 * var dt = new DateTime.now(); | |
| 48 * var dt2 = dt.toUtc(); | |
| 49 * dt == dt2; // => false | |
| 50 * dt.compareTo(dt2); // => 0 | |
| 51 * | |
| 52 * The [Comparable] interface does not imply the existence | |
| 53 * of the comparison operators `<`, `<=`, `>` and `>=`. | |
| 54 * These should only be defined | |
| 55 * if the ordering is a less-than/greater-than ordering, | |
| 56 * that is, an ordering where you would naturally | |
| 57 * use the words "less than" about the order of two elements. | |
| 58 * | |
| 59 * If the equality operator and [compareTo] disagree, | |
| 60 * the comparison operators should follow the equality operator, | |
| 61 * and will likely also disagree with [compareTo]. | |
| 62 * Otherwise they should match the [compareTo] method, | |
| 63 * so that `a < b` iff `a.compareTo(b) < 0`. | |
| 64 * | |
| 65 * The [double] class defines comparison operators | |
| 66 * that are compatible with equality. | |
| 67 * The operators differ from `double.compareTo` on -0.0 and NaN. | |
| 68 * | |
| 69 * The [DateTime] class has no comparison operators, instead it has the more | |
| 70 * precisely named [DateTime.isBefore] and [DateTime.isAfter]. | |
| 71 */ | |
| 72 abstract class Comparable<T> { | |
| 73 /** | |
| 74 * Compares this object to another [Comparable] | |
| 75 * | |
| 76 * Returns a value like a [Comparator] when comparing `this` to [other]. | |
| 77 * That is, it returns a negative integer if `this` is ordered before [other], | |
| 78 * a positive integer if `this` is ordered after [other], | |
| 79 * and zero if `this` and [other] are ordered together. | |
| 80 * | |
| 81 * The [other] argument must be a value that is comparable to this object. | |
| 82 */ | |
| 83 int compareTo(T other); | |
| 84 | |
| 85 /** | |
| 86 * A [Comparator] that compares one comparable to another. | |
| 87 * | |
| 88 * It returns the result of `a.compareTo(b)`. | |
| 89 * | |
| 90 * This utility function is used as the default comparator | |
| 91 * for ordering collections, for example in the [List] sort function. | |
| 92 */ | |
| 93 static int compare(Comparable a, Comparable b) => a.compareTo(b); | |
| 94 } | |
| OLD | NEW |