URI: 
       this algorithm seems to keep order for any scenario - dwm - dynamic window manager
  HTML git clone https://git.parazyd.org/dwm
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 016c54196e682ae8658854febb746b0437a010dc
   DIR parent 5056bb952ddd9d667d2897ab09324ef7ce1596ea
  HTML Author: Anselm R. Garbe <arg@10kloc.org>
       Date:   Tue, 29 Aug 2006 09:57:57 +0200
       
       this algorithm seems to keep order for any scenario
       Diffstat:
         M view.c                              |      55 +++++++++++++++++++++++--------
       
       1 file changed, 41 insertions(+), 14 deletions(-)
       ---
   DIR diff --git a/view.c b/view.c
       @@ -4,6 +4,31 @@
         */
        #include "dwm.h"
        
       +/* static */
       +
       +static Client *
       +getslot(Client *c)
       +{
       +        unsigned int i, tic;
       +        Client *p;
       +
       +        for(tic = 0; tic < ntags && !c->tags[tic]; tic++);
       +        for(p = clients; p; p = p->next) {
       +                for(i = 0; i < ntags && !p->tags[i]; i++);
       +                if(tic < i)
       +                        return p;
       +        }
       +        return p;
       +}
       +
       +static Client *
       +tail()
       +{
       +        Client *c;
       +        for(c = clients; c && c->next; c = c->next);
       +        return c;
       +}
       +
        /* extern */
        
        void (*arrange)(Arg *) = DEFMODE;
       @@ -11,27 +36,29 @@ void (*arrange)(Arg *) = DEFMODE;
        void
        attach(Client *c)
        {
       -        Client *first = getnext(clients);
       +        Client *p;
        
       -        if(!first) {
       -                if(clients) {
       -                        for(first = clients; first->next; first = first->next);
       -                        first->next = c;
       -                        c->prev = first;
       -                }
       -                else
       -                        clients = c;
       +        if(!clients) {
       +                clients = c;
       +                return;
       +        }
       +        if(!(p = getnext(clients)) && !(p = getslot(c))) {
       +                p = tail();
       +                c->prev = p;
       +                p->next = c;
       +                return;
                }
       -        else if(first == clients) {
       +
       +        if(p == clients) {
                        c->next = clients;
                        clients->prev = c;
                        clients = c;
                }
                else {
       -                first->prev->next = c;
       -                c->prev = first->prev;
       -                first->prev = c;
       -                c->next = first;
       +                p->prev->next = c;
       +                c->prev = p->prev;
       +                p->prev = c;
       +                c->next = p;
                }
        }