R, 5647 bytes
\(e,d,l,m=c(d,l))m[order(!(m<e)-(e<d),(m-e)^2)]
Approach copied from Jonah's answer - upvote that one!
Previously: R, 56 bytes
\(e,d,l,u=e<d,`+`=sort)c(c(d,l[i<-!(l>e)-u])+!u,l[!i]+u)
How?
Ungolfed code: uses Uses the decreasing
argument of sort
to control sorting order:.
f=function(e,d,l){ # e=entry, d=destination, l=list of passenger floors
u=e<d # u=boolean: TRUE=going up; FALSE=going down
i=!(l>e)-u # i=elements of l in the first direction
c( # join together the first & second directions
sort(c(d,l[i]) # sort the stops in the first direction...
,!u) # using 2nd arg of sort: decreasing TRUE/FALSE=!u
,sort(l[!i] # sort the stops in the second direction
,u) # in the other order: descending TRUE/FALSE=u
)