# Sorting predicates in PROLOG

**URL:** <https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [July 17, 2022, 7:59pm UTC](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618 "2022-07-17T19:59:59Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![elfi](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@elfi](https://swi-prolog.discourse.group/u/elfi)\
**Post date:** [August 2, 2022, 1:07am UTC](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618/22 "2022-08-02T01:07:16Z")

</div>

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].`

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [August 2, 2022, 1:54am UTC](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618/23 "2022-08-02T01:54:36Z")

</div>

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

```prolog
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)]).

```

---

<div class="post-metadata">

**Author:** ![elfi](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@elfi](https://swi-prolog.discourse.group/u/elfi)\
**Post date:** [August 2, 2022, 2:46am UTC](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618/24 "2022-08-02T02:46:42Z")

</div>

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 :

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

```

---

<div class="post-metadata">

**Author:** ![elfi](https://avatars.discourse-cdn.com/v4/letter/e/51bf81/32.png) [@elfi](https://swi-prolog.discourse.group/u/elfi)\
**Post date:** [August 3, 2022, 3:49pm UTC](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618/25 "2022-08-03T15:49:10Z")

</div>

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

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

```

[Previous page](https://swi-prolog.discourse.group/t/sorting-predicates-in-prolog/5618.md?page=1)
