Someone who wants to sell land or a house, and two come [to buy], each one saying: “I will buy it at such a price”, and neither of them is a next-door neighbour [who has right of first refusal (see below)], if one of them lives in his city, and the second is from a different city, the one from his city has precedence.
If both of them live in his city and one is his neighborhood, his neighbor has precedence.
If the second was a close friend [someone who is frequently at his house], and his neighbor is not at all a friend, his friend has precedence.
If one was his friend and the other his relative, his friend has precedence, as it is said (Prov. 27:63): “a close neighbor is better than a distant brother”. However, with any other people his relative has precedence, apart from a sage who has precedence — even over his neighbor or his friend who is often by him.
However, if one of them is his next-door neighbor, he has precedence over all of them. Even after he has sold to another, the next-door neighbor can give the money to the buyer and evict him. Even if the buyer is a sage, a neighbor and a relative, and the next-door neighbor is an unlearned peasant and socially distant from the seller, the next-door neighbor has precedence and can evict the buyer.
This system of precedences are commandments of the Sages, to carry out what was said (Devarim, 6:18): “And you shal do what is good and honest in G-d’s ‘Eyes’.”
I think there are two factors here. The first is to be fair to the next-door neighbor, who is much impacted by the sale. Aside from having a new neighbor, it also means an opportunity to expand his property.
The second is a matter of giving priority to someone close to you. If I may quote R’ Shimon Shkop (yes, yet again — you should be used to it by now!):
Although at first glance it seems that feelings of love for oneself and feelings of love for others are like competing co-wives [tzaros; the etymology meaning: troubles] one to the other, we have the duty to try to delve into it, to find the means to unite them, since Hashem expects both from us. This means [a person must] explain and accept the truth of the quality of his “I”, for with it the statures of [different] people are differentiated, each according to their level.
The entire “I” of a coarse and lowly person is restricted only to his substance and body. Above him is someone who feels that his “I” is a synthesis of body and soul. And above him is someone who can include in his “I” all of his household and family. Someone who walks according to the way of the Torah, his “I” includes the whole Jewish people, since in truth every Jewish person is only like a limb of the body of the nation of Israel. And there are more levels in this of a person who is whole, who can connect his soul to feel that all of the world and worlds are his “I”, and he himself is only one small limb in all of creation. Then, his self-love helps him love all of the Jewish people and [even] all of creation.