All trades are matched on a first-in-first-served basis, according to best price.

If you enter an order to buy shares, and five minutes later someone enters an order to buy shares at the same price, your order will be matched first - you are ahead of the second order in the line to be matched.

If someone enters a better price than you however (a lower price if you are selling, a higher price if
you are buying), they will move to the front of the price line and will be matched before you.