ETA: I'm thinking that my question needs some clarification. I don't want to sort my arrays. I want to be sure that a given array that is in order by a particular criterion, is also in order by another criterion. I made a graphic to illustrate. Each开发者_StackOverflow row is an array ordered by number. If the letters are also in order, the array passes the test.
Original Question
I have a parent class PhysicalCount with 2 properties: date
, count
. I also have subclasses of PhysicalCount: ClutchCount
, FryCount
and MatCount
. When I have a mixed array of PhysicalCounts and subclasses I need to verify (not set!) that the order matches these criteria:
- objects are in order by
date
- 0 to 1 objects of each child class may exist
- 0 to many objects of
PhysicalCount
may exist - if a
ClutchCount
is present it must have an earlier date than aFryCount
orMatCount
if those exist - if a
FryCount
is present it must have an earlier date than aMatCount
if that exists
Boiled down, the question is something like:
Given a list sorted by one criterion ($o->date
in my case), what is the most efficient way to ascertain that sorting that same list by another criterion (get_class($o)
in my case) will result in the same order?
I'd prefer a solution in PHP, but I'm thinking this is a fairly common problem that has a standard solution that I just don't know the name of. (Here is me regretting my degree choice [not CS]).
Based on our discussion in the comments, what you need is a sort that is somewhat "stable" (not actually a stable sort by definition though) use usort to sort the objects in the array in a user-defined way. You can specify in your $cmp_function
the criteria you mentioned in your question, so that the sorted array fits your needs.
精彩评论