For starters:
(defun c:layerforward()
(princ)
)
That's a start. Now, what you need to do is write a list of things in order of importance that need to happen, like: Collect all items in the layer table. If layer x or xy or xyz exist, draworder, select all objects on said layers, move to front. That or the inverse should work. I tend to find that using back tends to work better than front. However, you could accomplish this quite simply by using filter and then working through the draworder command, but that's not why you're here, is it?
Now, a thought from my friend Paul Simon: "The problem is all inside your head, she said to me. The answer is easy if you take it logically. I'd like to help you in your struggle to be free. There must be 50 ways..." to write your code. I'm sure you'll get more help as you progress, but the best thing is to learn to write your code out in plain language first. IOW, what are the steps that need to be taken before writing any code.