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 9236109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:12:36+00:00 2026-06-18T07:12:36+00:00

Is there any way to maintain a sorted array of objects? For example, if

  • 0

Is there any way to maintain a sorted array of objects?

For example, if I have an object with properties ID, Date, Name and a collection of these objects:

$col = array();

public function addNewObject($id, $date, $name)
{
    $col[] = new Object($id, $date, $name); 
    //but instead of appending, it should place it by Name desc
}

If I call something like getObjects, it would return the items in the collection by Name desc.

I think there were some answers for getting objects back in a sorted order, but for efficiency, I would think it be better to sort at insert as the “sort by” variable in my case will never change.

UPDATE:
So based on the comments, I should resort the whole array each time something is added but that seems a bit memory intensive…

Since the array would always be in sorted order to start out with I can identify the location where I want to insert by traversing the array (would this be efficient, is there a better way?). Once I find that how could I “insert” a new object into the array?

I do not imagine that the array will be very large but I would like to implement this the most efficient way possible.

  • 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-18T07:12:37+00:00Added an answer on June 18, 2026 at 7:12 am

    If you’re not keen on resorting the array after you add (although I’d recommend it; realistically this wont be a performance issue and it keeps the code readable.

    However, if you definitely don’t want to do this then you can, as you said, traverse the array and find out where to insert:

    $col = array();
    
    public function addNewObject($id, $date, $name){
        //Find the index to insert at
        $index = 0;
        foreach($col as $i => $item){
            if($item->name > $name){
               //This item is after the item we want to insert. 
               //Use the previous index and stop traversing
               break;
            }
            $index = $i;
        }
        $col = array_splice($col, $index, 0,  new Object($id, $date, $name));
    }
    

    Using array_splice to insert at an arbritary position thanks to https://stackoverflow.com/a/3797526/505722

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

Sidebar

Related Questions

I have a nested javascript object called data. Is there any way to iterate
If I have a Cursor object for a query, is there any way of
Is there any way in which I can automatically convert a Custom Class Object
Is there any way to maintain the same functionality in the code below, but
Simple version Is there any way to maintain focus on menu after clicking a
I have a collection of objects in JavaScript like this: Object collection = new
Is there any way to maintain stream from php to the jabber server without
Is there any way to have Excel 2007 connect to a table or view
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert

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.