Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eokorie's avatar

Sorting a collection by priority and date

Hi All,

I am looking for a bit of assistance on a small issue if some good folks have the time.

Take the result of this collection below.

array:6 [
  0 => {#710
    +"chatConversationId": 110
    +"startTime": "2017-02-22T11:14:46.567"
    +"summary": "Hello from a patient"
    +"priorityTag": "Low"
  }
  1 => {#709
    +"chatConversationId": 112
    +"startTime": "2017-02-22T11:14:46.72"
    +"summary": null
    +"priorityTag": "Medium"

  }
  2 => {#700
    +"chatConversationId": 113
    +"startTime": "2017-02-22T11:15:25.15"
    +"summary": null
    +"priorityTag": "Low"

  }
  3 => {#701
    +"chatConversationId": 114
    +"startTime": "2017-02-22T11:15:25.37"
    +"summary": null
    +"priorityTag": "High"
  }
  4 => {#702
    +"chatConversationId": 115
    +"startTime": "2017-02-22T11:15:25.493"
    +"summary": null
    +"priorityTag": "High"
  }
  5 => {#703
    +"chatConversationId": 116
    +"startTime": "2017-02-22T11:15:25.557"
    +"summary": null
    +"priorityTag": null
  }
]

How do I order this collection so that the items are ordered by priority and by date ascending? In effect what I would like would be the results to look like this:

array:6 [
  
  0 => {#701
    +"chatConversationId": 114
    +"startTime": "2017-02-22T11:15:25.37"
    +"summary": null
    +"priorityTag": "High"
  }
  1 => {#702
    +"chatConversationId": 115
    +"startTime": "2017-02-22T11:15:25.493"
    +"summary": null
    +"priorityTag": "High"
  }
  2 => {#709
    +"chatConversationId": 112
    +"startTime": "2017-02-22T11:14:46.72"
    +"summary": null
    +"priorityTag": "Medium"
  }
  3 => {#700
    +"chatConversationId": 113
    +"startTime": "2017-02-22T11:15:25.15"
    +"summary": null
    +"priorityTag": "Low"

  }
4 => {#710
    +"chatConversationId": 110
    +"startTime": "2017-02-22T11:14:46.567"
    +"summary": "Hello from a patient"
    +"priorityTag": "Low"
  }
  5 => {#703
    +"chatConversationId": 116
    +"startTime": "2017-02-22T11:15:25.557"
    +"summary": null
    +"priorityTag": null
  }
]

I have attempted to use the sortBy and sortByDesc methods either on their own or combined and the results come back incorrect.

$conversations->filter(function ($conversation) {
            return (string)$conversation->status === 'Open';
        })->sortBy(function ($conversation) {
            return $conversation->priorityTag;
        })->sortBy(function ($conversation) {
            return $conversation->startTime;
        })->values()->all();

Now I could just be doing this incorrectly, but any ideas would be very helpful... Thanks...

0 likes
1 reply
eokorie's avatar

After a bit of digging around and some experimenting, this is what I have come up with:

$conversations->filter(function ($conversation) {
            return (string)$conversation->status === 'Open';
        })->sort(function ($a, $b) {
            if ($a->priorityTag === $b->priorityTag) {
                if ($a->startTime === $b->startTime) {
                    return 0;
                }
                return $a->startTime < $b->startTime ? -1 : 1;
            }
            return $a->priorityTag > $b->priorityTag ? -1 : 1;
        })

The above seems to be returning what I want, but it's putting the medium priorities before the high ones, when it should be the other way round...

Please or to participate in this conversation.