diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 96c26bc102..12d8d59f41 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -252,7 +252,7 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) { if !is_reverse && sym.has_method('<') && infix_expr.left.str().len == 1 { g.definitions.writeln('\tif (${styp}__lt(*a, *b)) { return -1; } else { return 1; }}') } else if is_reverse && sym.has_method('<') && infix_expr.left.str().len == 1 { - g.definitions.writeln('\tif (!${styp}__lt(*a, *b)) { return -1; } else { return 1; }}') + g.definitions.writeln('\tif (${styp}__lt(*b, *a)) { return -1; } else { return 1; }}') } else { field_type := g.typ(infix_expr.left_type) mut left_expr_str := g.write_expr_to_string(infix_expr.left) diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 2c2fdc9450..1312774848 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -291,7 +291,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } p.next() } else if p.tok.kind in [.ne, .gt, .ge, .le] && p.peek_tok.kind == .lpar { - p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated with `==` and`<`', + p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`', p.tok.position()) } else { pos := p.tok.position() diff --git a/vlib/v/tests/operator_overloading_cmp_test.v b/vlib/v/tests/operator_overloading_cmp_test.v index 1c94d45747..10129f9958 100644 --- a/vlib/v/tests/operator_overloading_cmp_test.v +++ b/vlib/v/tests/operator_overloading_cmp_test.v @@ -13,6 +13,7 @@ fn (a Foo) == (b Foo) bool { fn test_operator_overloading_cmp() { a := Foo{i: 38} b := Foo{i: 38} + mut arr := [a, b] assert (a > b) == false assert (a < b) == false @@ -22,4 +23,9 @@ fn test_operator_overloading_cmp() { //// /// // assert b >= a assert b <= a + //// /// // + arr.sort(a > b) + assert arr[0].i == 38 + arr.sort(a < b) + assert arr[0].i == 38 }