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