Sorting predicates in PROLOG

Thanks so much !

I managed to find the patients on time and late,
there I have a list of patients with their states:

ListP = [patient(201, 4, 2, inTime), patient(202, 3, 2, late), patient(203, 2, 3, late), patient(204, 1, 3, late), patient(204 , 1, 4, inTime),patient(203, 2, 4, late), patient(204, 1, 5, inTime),patient(204, 1, 5, inTime)].

I want to have the number of patients in time for the patients classified by their 3rd argument

in the previous example I have
patient(201, 4, 2, inTime), patient(202, 3, 2, late) so I have only one patient in time and who has as 3rd argument 2, the same thing for other patients and so on…

The result list for the previous example should be:

ResList = [1, 0, 1, 2].

If the data are in a list, you can make them appear to be like a predicate by using member/3, e.g.:

patient(P) :-
    member(P, [patient(201, 4, 2, inTime), 
               patient(202, 3, 2, late), 
               patient(203, 2, 3, late), 
               patient(204, 1, 3, late), 
               patient(204, 1, 4, inTime),
               patient(203, 2, 4, late),
               patient(204, 1, 5, inTime),
               patient(204, 1, 5, inTime)]).

my actual data now is in a list as showen in the last exemple :

ListP = [patient(201, 4, 2, inTime), patient(202, 3, 2, late), patient(203, 2, 3, late), patient(204, 1, 3, late), patient(204 , 1, 4, inTime),patient(203, 2, 4, late), patient(204, 1, 5, inTime),patient(204, 1, 5, inTime)].

I edited my text hoping my problem will be more clear.

Isn’t possible to do it with lists ? i shoul make them appear to be like a predicate ?

what i tried :

count_occurrences(List, Occ):-
    findall([patient(_,_,X,aTemps),L], (bagof(true,member(patient(_,_,X,aTemps),List),Xs), length(Xs,L)), Occ).

How can i add the index of each line of the list :

1 301 1 2
2 201 5 2
3 401 1 4
4 501 1 5