sorting list of lists by the second element of each sublist prolog
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).
精彩评论