# Order_by/2 examples

Here are some examples for order_by/2 from library(solution_sequences) as test cases.

``````:- begin_tests(order_by).

% run_tests(order_by).

data(0, 1, 1).
data(1, 0, 0).
data(0, 0, 0).
data(1, 1, 0).
data(1, 0, 1).
data(0, 0, 1).
data(1, 1, 1).
data(0, 1, 0).

% Bag ordering: same order as data
% Item output format: List
% Item element order: A,B,C
% Data source: facts
test(1,[ all( Cs == [[[0,1,1],[1,0,0],[0,0,0],[1,1,0],[1,0,1],[0,0,1],[1,1,1],[0,1,0]]] ) ] ) :-
bagof([A,B,C], data(A, B, C), Cs).

% Bag ordering: same order as data
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(2,[ all( Cs == [[(0,1,1),(1,0,0),(0,0,0),(1,1,0),(1,0,1),(0,0,1),(1,1,1),(0,1,0)]] ) ] ) :-
bagof((A,B,C), data(A, B, C), Cs).

% Bag ordering: same order as data
% Item output format: Compound with functor (-)
% Item element order: A,B,C
% Data source: facts
test(3,[ all( Cs == [[0-1-1,1-0-0,0-0-0,1-1-0,1-0-1,0-0-1,1-1-1,0-1-0]] ) ] ) :-
bagof(A-B-C, data(A, B, C), Cs).

% Bag ordering: asc(A)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(4,[ all( (A,B,C) == [(0,1,1),(0,0,0),(0,0,1),(0,1,0),(1,0,0),(1,1,0),(1,0,1),(1,1,1)] ) ]  ) :-
order_by([asc(A)], data(A,B,C)).

% Bag ordering: asc(B)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(5,[ all( (A,B,C) == [(1,0,0),(0,0,0),(1,0,1),(0,0,1),(0,1,1),(1,1,0),(1,1,1),(0,1,0)] ) ]  ) :-
order_by([asc(B)], data(A,B,C)).

% Bag ordering: asc(C)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(6,[ all( (A,B,C) == [(1,0,0),(0,0,0),(1,1,0),(0,1,0),(0,1,1),(1,0,1),(0,0,1),(1,1,1)] ) ]  ) :-
order_by([asc(C)], data(A,B,C)).

% Error
% Order specifier can have only one variable
test(7,[error(domain_error(order_specifier,asc(_,_)),_)]  ) :-
order_by([asc(A,C)], data(A,_,C)).

% Bag ordering: asc(A),asc(C)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(8,[ all( (A,B,C) == [(0,0,0),(0,1,0),(0,1,1),(0,0,1),(1,0,0),(1,1,0),(1,0,1),(1,1,1)] ) ]  ) :-
order_by([asc(A),asc(C)], data(A,B,C)).

% Bag ordering: asc(A),asc(B),asc(C)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(9,[ all( (A,B,C) == [(0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0),(1,0,1),(1,1,0),(1,1,1)] ) ]  ) :-
order_by([asc(A),asc(B),asc(C)], data(A,B,C)).

% Bag ordering: asc(C),asc(B),asc(A) is same as asc(A),asc(B),asc(C)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(10,[ all( (A,B,C) == [(0,0,0),(1,0,0),(0,1,0),(1,1,0),(0,0,1),(1,0,1),(0,1,1),(1,1,1)] ) ]  ) :-
order_by([asc(C),asc(B),asc(A)], data(A,B,C)).

% Bag ordering: asc(C),desc(B),asc(A)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: facts
test(11,[ all( (A,B,C) == [(0,1,0),(1,1,0),(0,0,0),(1,0,0),(0,1,1),(1,1,1),(0,0,1),(1,0,1)] ) ]  ) :-
order_by([asc(C),desc(B),asc(A)], data(A,B,C)).

% Bag ordering: asc(C),asc(B),asc(A)
% Item output format: Compound with functor (,)
% Item element order: A,B,C
% Data source: List
test(12,[ all( (A,B,C) == [(0,0,0),(1,0,0),(0,1,0),(1,1,0),(0,0,1),(1,0,1),(0,1,1),(1,1,1)] ) ]  ) :-
List = [[0,1,1],[1,0,0],[0,0,0],[1,1,0],[1,0,1],[0,0,1],[1,1,1],[0,1,0]],
order_by([asc(C),asc(B),asc(A)], member([A,B,C],List)).

% Bag ordering: asc(C),asc(B),asc(A)
% Item output format: Structure item(A,B,C)
% Item element order: A,B,C
% Data source: facts
test(13,[ all( item(A,B,C) == [item(0,0,0),item(1,0,0),item(0,1,0),item(1,1,0),item(0,0,1),item(1,0,1),item(0,1,1),item(1,1,1)] ) ]  ) :-
order_by([asc(C),asc(B),asc(A)], data(A,B,C)).

:- end_tests(order_by).
``````

Example run

``````?- run_tests(order_by).
% PL-Unit: order_by ............. done
% All 13 tests passed
true.
``````