cgen: fix gen_array_sort() (#8077)
parent
254df0ca62
commit
dd6febf6fa
|
@ -254,9 +254,9 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
|
|||
// when generating the function as long as the args are named the same.
|
||||
g.definitions.writeln('int $compare_fn ($styp* a, $styp* b) {')
|
||||
sym := g.table.get_type_symbol(typ)
|
||||
if sym.has_method('<') && infix_expr.left.str().len == 1 {
|
||||
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 sym.has_method('>') && infix_expr.left.str().len == 1 {
|
||||
} else if is_reverse && sym.has_method('>') && infix_expr.left.str().len == 1 {
|
||||
g.definitions.writeln('\tif (${styp}__gt(*a, *b)) { return -1; } else { return 1; }}')
|
||||
} else {
|
||||
field_type := g.typ(infix_expr.left_type)
|
||||
|
|
|
@ -11,6 +11,10 @@ fn (p Parent) < (p1 Parent) bool {
|
|||
return p.name < p1.name
|
||||
}
|
||||
|
||||
fn (p Parent) > (p1 Parent) bool {
|
||||
return p.name > p1.name
|
||||
}
|
||||
|
||||
fn test_sorting_by_different_criteria_in_same_function() {
|
||||
mut arr := [
|
||||
Parent{Child{0.2}, 'def'},
|
||||
|
@ -26,4 +30,6 @@ fn test_sorting_by_different_criteria_in_same_function() {
|
|||
assert arr[0].name == 'xyz'
|
||||
arr.sort(a < b)
|
||||
assert arr[0].name == 'abc'
|
||||
arr.sort(a > b)
|
||||
assert arr[0].name == 'xyz'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue