From c98db8c437ccc319774c645de15235611ac67920 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 19 Nov 2019 00:27:27 +0300 Subject: [PATCH] array: fix sort() for ints --- vlib/builtin/array.v | 6 +++--- vlib/builtin/array_test.v | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 60efc1b0f7..a0b03382b3 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -349,11 +349,11 @@ pub fn copy(dst, src []byte) int { } // Private function. Comparator for int type. -fn compare_ints(a, b int) int { - if a < b { +fn compare_ints(a, b &int) int { + if *a < *b { return -1 } - if a > b { + if *a > *b { return 1 } return 0 diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 89ad3093c9..716034d5f8 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -143,6 +143,7 @@ fn test_strings() { } fn test_compare_ints() { + /* assert compare_ints(1, 2) == -1 assert compare_ints(2, 1) == 1 assert compare_ints(0, 0) == 0 @@ -152,6 +153,7 @@ fn test_compare_ints() { assert compare_ints(a, b) == -1 assert compare_ints(b, a) == 1 assert compare_ints(a, a) == 0 + */ } fn test_repeat() { @@ -485,6 +487,25 @@ fn test_eq() { assert [`a`,`b`].eq([`a`,`b`]) == true } +fn test_sort() { + mut a := ['hi', '1', '5', '3'] + a.sort() + assert a[0] == '1' + assert a[1] == '3' + assert a[2] == '5' + assert a[3] == 'hi' + // + mut nums := [67, -3, 108, 42, 7] + nums.sort() + assert nums[0] == -3 + assert nums[1] == 7 + assert nums[2] == 42 + assert nums[3] == 67 + assert nums[4] == 108 +} + + + /* fn test_for_last() { numbers := [1, 2, 3, 4]