开发者

sorting list of lists by the second element of each sublist prolog

开发者 https://www.devze.com 2023-02-19 11:22 出处:网络
I have a list like L = [[id1,avg1],[id2,avg2],....,[idN,avgN]] and I want to sort this list by avg. I can\'t even start coding!

I have a list like L = [[id1,avg1],[id2,avg2],....,[idN,avgN]] and I want to sort this list by avg. I can't even start coding! Any ide开发者_如何学Goa???


You probably want to look into predsort. For this you need to create a predicate that does comparisons for your list items.
For example:

compareAvg(X, [_,A1], [_,A2]) :- compare(X, A1, A2).

then you can call

predsort(compareAvg, [[1,2],[3,1],[5,3]],X).

0

精彩评论

暂无评论...
验证码 换一张
取 消