Question: Okay, a before;

```
Array (
    'home' => array('order' => 1),
    'about' => array(),
    'folio' => array('order' => 2),
    'folio/web' => array('order' => 2),
    'folio/print' => array('order' => 1)
    'contact' => array('order' => 2)
)

```

And a desired after;

```
Array (
    'home' => array('order' => 1),
    'contact' => array('order' => 2),
    'folio' => array('order' => 2),
    'folio/print' => array('order' => 1),
    'folio/web' => array('order' => 2),
    'about' => array()
)

```

I know, horrific (don't ask!)

See how the slash in the key indicates children, and how the order is nested accordingly? And items without orders are simply shifted to the bottom.

But also how multiple 'same level' items with the same order are merely sorted by key?

Answer: 
@Timothy, your answer works, but it generates duplicates:

```
?- length(S,_), s(S,[]).
S = [] ;
S = [a, d] ;
S = [a, d] ;            % XXX
S = [b, b, c, c] ;
S = [a, a, d, d] ;
S = [a, a, d, d] ;      % XXX

```

This can be fixed by removing one clause, leaving the DCG:

```
s --> x.
s --> a,s,d.

x --> [].
x --> b,b,x,c,c.

% a, b, c, d the same

```

This generates:

```
?- length(S,_), s(S,[]).
S = [] ;
S = [a, d] ;
S = [b, b, c, c] ;
S = [a, a, d, d] ;
S = [a, b, b, c, c, d] ;
S = [a, a, a, d, d, d] ;
S = [b, b, b, b, c, c, c, c] ;
S = [a, a, b, b, c, c, d, d] ;
S = [a, a, a, a, d, d, d, d] ;

```
I believe I figured it out...

```
s --> x.
s --> a,d.
s --> a,s,d.

x --> [].
x --> b,b,x,c,c.

a --> [a].
b --> [b].
c --> [c].
d --> [d].

?- s([],[]).
Yes

?- s([a,b,c,c,d],[]).
No

?- s([a,a,a,b,b,c,c,d,d,d],[]).
Yes

```

It's amusing to look at the solution and think, "I racked my brain over *that*?" But I guess that's half the fun of learning something new, especially when it's something like logic programing coming from an imperative programming background.