This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/regression/dados/order_by.pl

33 lines
846 B
Prolog

% taken from http://stackoverflow.com/questions/12272888/default-prolog-predicate-sort
/* File: order_by.pl
Author: Carlo,,,
Created: Sep 5 2012
Purpose: sort fact
*/
:- module(order_by,
[order_by/2
]).
:- use_module(library(apply_macros)).
order_by(PredicateIndicator, Argument) :-
( PredicateIndicator = Module:Functor/Arity
; PredicateIndicator = Functor/Arity, Module = user
),
length(EmptyArgs, Arity),
P =.. [Functor|EmptyArgs],
findall(P, retract(Module:P), L),
predsort(by_arg(Argument), L, S),
maplist(assert_in_module(Module), S).
assert_in_module(Module, P) :-
assertz(Module:P).
by_arg(Argument, Delta, E1, E2) :-
arg(Argument, E1, A1),
arg(Argument, E2, A2),
( A1 @< A2
-> Delta = <
; Delta = >
).