Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a social networking app that uses a codeigniter REST api and one of our features is a feed of newly created content and up until now this has only consisted of user statuses, but now id like to add a variety of different content into the feed( updates,photos, user interactions, etc.)

I have functions in place that grab each type of content individually and places them in array of objects. So there is an array of object of statuses, an array of objects for photos and etc. Each object in each of these arrays of objects has a timestamp field and I would like to merge all of these arrays of objects into a single array of objects sorted by the timestamp field all the objects share, but im not sure how to go about doing this?

share|improve this question
    
where is your code, what have you tried? –  steven Sep 18 '13 at 19:42
    
do all of the objects have a timestamp field? –  watcher Sep 18 '13 at 19:47
    
To be honest I have no idea how to achieve my desired effect .Regarding code if i were to post code it would only be a codeigniter controller containing several arrays of objects stored in variables which i dint think would be to helpful, but I could post 2 dummy arrays of objects if it would help you visualize it? –  ChuckKelly Sep 18 '13 at 19:49

1 Answer 1

up vote 0 down vote accepted

You can leverage usort for the sorting, as long as each object has a public timestamp field

function compareObjTimestamps( $a, $b ) {
    return $a->timestamp - $b->timestamp;
}

usort( $objArray, 'compareObjTimestamps' );

For the merging, following this approach just lump all of the objects into a single array prior to the call to usort.

Example

<?php

class A {
    public $sortField;

    public function __construct( $sortField ) {
        $this->sortField = $sortField;
    }
}

class B {
    public $sortField;

    public function __construct( $sortField ) {
        $this->sortField = $sortField;
    }

}

function compare( $a, $b ) {
    return $a->sortField - $b->sortField;
}


$test = array(
    new A(1),
    new B(2),
    new A(3),
    new B(4),
    new B(1),
    new A(17),
    new B(10),
);

usort( $test, 'compare' );

foreach( $test as $entry ) {
    echo $entry->sortField . ': ' . get_class( $entry ) . "\n";
}

Multiple iterations of this script produced the following output:

1: B 
1: A 
2: B 
3: A 
4: B 
10: B 
17: A

So it looks like that whatever appears later in the input will be first in the output, although I can't find any documentation on this. The manual simply states:

4.1.0 A new sort algorithm was introduced. The cmp_function doesn't keep the original order for elements comparing as equal.

share|improve this answer
    
This seems like a decent solution , but what would happen if the two timestamps were identical? –  ChuckKelly Sep 18 '13 at 20:42
    
Check the manual entry for usort. The compare function is intended to return either a negative number, 0 or a positive number indicating the relative order between the two arguments. If they are the same the function will return 0 (indicating to usort that, for the purposes of comparison sorting, the two objects are the same) –  watcher Sep 18 '13 at 20:47
    
I suppose this is a completly different question, but would you have any idea how i would go about removing duplicate items ( i would verify they are duplicates by a content_id field) –  ChuckKelly Sep 18 '13 at 20:56
    
I think you should try not to have duplicates in the first place. You wouldnt want to remove the duplicates within the compare function, but if you must have duplicates in the input you'll have to iterate over it and within the iteration iterate over the rest of the list to see if the current element is duplicated. –  watcher Sep 18 '13 at 21:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.