如何按功能比较排序:cmp?
type Point =
{
x : float
y : float
}
let s = ResizeArray<Point>()
s.Add{x=1.1 ; y=1.1}
s.Add{x=2.2 ; y=2.2}
s.Add{x=3.3 ; y=3.3}
s.Add{x=2.2 ; y=2.0}
let cmp (A:Point) (B:Point) =
A.x + A.y < B.x + B.y
s.Sort()
//s.Sort()
s |> printfn("%A")
这个结果,但功能不正确:cmp
seq [{x = 1.1;
y = 1.1;}; {x = 2.2;
y = 2.2;}; {x = 2.2;
y = 2.0;}; {x = 3.3;
y = 3.3;}]
我尝试使用 sortWith ,但仍然不正确。你能帮我解决一下吗?
type Point =
{
x : float
y : float
}
let s = ResizeArray<Point>()
s.Add{x=1.1;y=1.1}
s.Add{x=2.2;y=2.2}
s.Add{x=1.0;y=1.0}
let cmp (A:Point) (B:Point) =
if (A.x+A.y) >= (B.x+B.y) then 1
else 0
List.sortWith cmp s |> List.iter (fun x -> printfn("%f %f\n") x.x x.y)