thinking about it, i'm not even that sure how the orders are matched. i guess there are certain rules, like:
1. all stock in a single order must be bought be sold/bought for the same price
2. an order cannot be executed unless each stock in the order is sold/bought
3. there are different types of order - limit (sell/buy only once a min/max is reached) and market (sell/buy for whatever is best price)
4. stocks can be sold to/bought from multiple buyers/sellers in order to execute an order
5. if multiple identical orders, then execute first order first (but what about when non-identical and later order can be matched but first order cannot as different number of stock in the order?)
lots of interesting concepts here but couldn't find much online