1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

array: fix sort() for ints

This commit is contained in:
Alexander Medvednikov 2019-11-19 00:27:27 +03:00
parent c73f34cc5f
commit c98db8c437
2 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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]