Fix how we use Comparable.compare, so we have a function with the appropriate type in JSArray.sort

This is the same fix that was done here: https://github.com/dart-lang/sdk/blob/master/sdk/lib/collection/list.dart#L349

Change-Id: I6013dc96c67b487b0e96118028ef920a84f371b5
Reviewed-on: https://dart-review.googlesource.com/52701
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Sigmund Cherem 2018-04-26 00:32:02 +00:00 committed by commit-bot@chromium.org
parent 30b53ceaab
commit 004ee9cd12
2 changed files with 16 additions and 1 deletions

View file

@ -523,7 +523,13 @@ class JSArray<E> extends Interceptor implements List<E>, JSIndexable {
void sort([int compare(E a, E b)]) {
checkMutable('sort');
Sort.sort(this, compare == null ? Comparable.compare : compare);
Sort.sort(this, compare ?? _compareAny);
}
static int _compareAny(a, b) {
// In strong mode Comparable.compare requires an implicit cast to ensure
// `a` and `b` are Comparable.
return Comparable.compare(a, b);
}
void shuffle([Random random]) {

View file

@ -0,0 +1,9 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Tests that the default comparable function in JSArray.sort has a valid
/// strong-mode type.
void main() {
new List<dynamic>.from(['1', '2']).sort();
}