checks the first two and the last two elements to decide on sort function