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.

Rachel Gallen
  • 25,819
  • 19
  • 69
  • 75
allanx2000
  • 684
  • 3
  • 9
  • 21
  • you can clone an array object to maintain its old order and rearrange the clone one. – thegrunt Feb 03 '13 at 18:33
  • 1
    A way to maintain such order is to resort whole array after adding new item. – dev-null-dweller Feb 03 '13 at 18:35
  • Building on @thegrunt 's answer, if you sort the array after adding the element, you will have it sorted when you need it. Since you always want it sorted the same way, it means that multiple accesses to the array don't result in multiple sorts. Inserting in the right place will require a bit of "sorting" (namely, figuring out where the new element goes) anyway. May not be worth the extra work on your part - depending on the size of your array. – Floris Feb 03 '13 at 18:36
  • 1
    Also, the difference is that your extra code to find the position and insert the new item will be in PHP, while the sort API is in C... – Breaking not so bad Feb 03 '13 at 19:01

2 Answers2

1

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

Community
  • 1
  • 1
Jim
  • 21,521
  • 5
  • 49
  • 80
0

this is a good example of a function that sorts an array from whatever key you want it sorted by

http://www.php.net/manual/en/function.sort.php#99419

in your example you should run it like :

  array_sort($col, 'Name', SORT_DESC)); 

take in mind that every time you add a new item to the array the whole array is sorted each time

Nimrod007
  • 9,075
  • 8
  • 43
  • 68