It looks like "order" function is not working properly with repeated values. For example, check the code below. As you can see, same elemets have a different order.