Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7715829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:29:05+00:00 2026-06-01T02:29:05+00:00

I would love to sort an embedded MongoDB object using PHP Lithium. I have

  • 0

I would love to sort an embedded MongoDB object using PHP Lithium. I have got a Model “Thread” which looks almost like this:

{
   "_id": ObjectId("4f71bf4618b602580f000009"),
   "postings": [
      {text: "a", upvotes: 15, /*...*/},
      {text: "b", upvotes: 23},
      {text: "c", upvotes: 16},
      {text: "d", upvotes: 42}
   ],
   // bla
}

Now I would like to sort the postings depending on their upvotes. I already wrote a method which roughly does what I want:

public function postings_sort_by_upvotes($thread) {
    $postings = $thread->postings->to('array');
    usort($postings, function($a, $b) {
                         return ($a['upvotes'] > $b['upvotes'] ? 1 : -1);
                     });
    return $postings;
}

This works, but obviously it returns the postings as a plain array, while the unsorted postings were a lithium\data\collection\DocumentArray type.

Do I really need to struggle with an array instead of an object or is there a way which allows me to sort them without losing the original data type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:29:07+00:00Added an answer on June 1, 2026 at 2:29 am

    A DocumentArray object is a Collection, and hopefully, Lithium Collections are sortable.
    You can call sort() on a $collection different ways :

    $collection->sort('field'); //sort naturally by the given field
    

    Or define a custom closure :

    $collection->sort(function ($a,$b) {
        if ($a == $b) {
            return 0;
        }
        return ($b > $a ? 1 : -1);
    });
    

    Check docs on lithium\data\Collection, from which DocumentArrayinherits, and lithium\util\Collection, the Collection object.

    an Introduction to Collectionsby Joe Beeson. He don’t cover sorting especially but it worth a read (other articles too)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is an elegant way to sort objects in PHP? I would love to
I would love to be able to have a common set of Classes in
I would love to get some tips from other people that have had this
I just started getting into BizTalk at work and would love to keep using
I am fairly new to php and would love to hear your thoughts on
I am working on generating emails using Java. I would love to use some
I have an NSArray of Object that has an interesting property that I would
How do I do a Quick Sort in Ada? I would also love a
New to PHP, would love to get to know it better. Currently working on
I would love to be able to do >>> A = numpy.array(((1,2),(3,4))) >>> idx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.