# [R](https://www.r-project.org), 47 bytes

```r
\(e,d,l,m=c(d,l))m[order(!(m<e)-(e<d),(m-e)^2)]
```

[Attempt This Online!](https://ato.pxeger.com/run?1=fY07DsIwEETrcArodqVZkU1iAVJyEX5NvK6wkCw4DY1B4lDcBkehpplXvBnN45nyKwzv-y3I9rM-ksHjgjiMVMgcD9fkLdGKYm8sZL1nUBTjc8On32wfqMayRVWN1EEaiEOJDgrHvJis_LEOWubFKlpssIPq1BetmeeHnGd-AQ)

Approach copied from [Jonah's answer](https://codegolf.stackexchange.com/a/258149/95126) - upvote that one!  

---
Previously: **[R](https://www.r-project.org), 56 bytes**

```r
\(e,d,l,u=e<d,`+`=sort)c(c(d,l[i<-!(l>e)-u])+!u,l[!i]+u)
```

[Attempt This Online!](https://ato.pxeger.com/run?1=fY1BCsIwFETX9RRml0_nQ2MbrNB4ELVQSBMoFAq1OY2bKHgob2OKXbuZgXkzzOM5x5c377B4rj_1TTr0GBGMa3p0eWfu07yQlVam-Do0LOR4dsShpVyEFImhzQNt-4uXBfYlsszKCnwAaySpoKCJdivlP1RDpXmiCiWOOEGptc-qoO0hxp9_AQ)

Uses the ```decreasing``` argument of ```sort``` to control sorting order.