ソースを参照

Treat custom comparator return values correctly (fixes #15).

Dmitry Panov 8 年 前
コミット
d9f9d94853
2 ファイル変更20 行追加1 行削除
  1. 1 1
      builtin_array.go
  2. 19 0
      runtime_test.go

+ 1 - 1
builtin_array.go

@@ -872,7 +872,7 @@ func (a *arraySortCtx) Len() int {
 }
 
 func (a *arraySortCtx) Less(j, k int) bool {
-	return a.sortCompare(a.obj.sortGet(int64(j)), a.obj.sortGet(int64(k))) == -1
+	return a.sortCompare(a.obj.sortGet(int64(j)), a.obj.sortGet(int64(k))) < 0
 }
 
 func (a *arraySortCtx) Swap(j, k int) {

+ 19 - 0
runtime_test.go

@@ -706,6 +706,25 @@ func TestJsonEncodable(t *testing.T) {
 	}
 }
 
+func TestSortComparatorReturnValues(t *testing.T) {
+	const SCRIPT = `
+	var a = [];
+	for (var i = 0; i < 12; i++) {
+	    a[i] = i;
+	}
+
+	a.sort(function(x, y) { return y - x });
+
+	for (var i = 0; i < 12; i++) {
+	    if (a[i] !== 11-i) {
+		throw new Error("Value at index " + i + " is incorrect: " + a[i]);
+	    }
+	}
+	`
+
+	testScript1(SCRIPT, _undefined, t)
+}
+
 /*
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)