How can I sort a nested list accordign to each element in nested lists
<p>I have a model Page which stores an optional parent page (instance of itself). In my views I made a function which will return a nested list of all pages and their parents.</p>

<p>For example, my page architecture is</p>

<pre><code>CCC
  AAA
    DDD
    KKK
      EEE
ZZZ
  BBB
</code></pre>

<p>So DDD has the parent page AAA, which has it's own parent page CCC. The CCC is top page and has no parent page.</p>

<p>The function will first get a queryset of all instances of Pages and sort them alphabetically. Then it will proceed to recursively generate a "full parent architecture" list, where each element on that list is another list of all parent pages, including the page itself. From the example above if we take a slice of list for page DDD, it would return [CCC, AAA, DDD].</p>

<p>My function currently returns a list like this for the above stated example:</p>

<pre><code>[ 
  [CCC, AAA],
  [ZZZ, BBB],
  [CCC],
  [CCC, AAA, DDD],
  [CCC, AAA, KKK, EEE],
  [CCC, AAA, KKK],
  [ZZZ],
]
</code></pre>

<p>As you can see from the that list, all elements are sorted alphabetically according to the last element on that list. Now I want to display all those parent pages on my front end to basically look like a sitemap and show the proper parent architecture of all Pages on my site that is sorted alphabetically according to each element in each nested list. The end result would be:</p>

<pre><code>[ 
  [CCC],
  [CCC, AAA],
  [CCC, AAA, DDD],
  [CCC, AAA, KKK],
  [CCC, AAA, KKK, EEE],
  [ZZZ],
  [ZZZ, BBB],
]
</code></pre>

<p>To put it simple, I want to go through each first element on each list and sort it alphabetically, then each second element and sort them as well, then third, then forth, and so on. Is there a way to do this?</p>

<p>EDIT:
To avoid confusion here's my view:</p>

<pre><code>def page_list(request):


    # Fetch all pages and sort them alphabetically
    queryset = Page.objects.all().order_by("title")
    output = []

    # Generate list of pages and their parents
    for page in queryset:
        output.append(get_list_of_parents(page))

    context = {
        "title": "Page List",
        "page_list": output,
    }

    return render(request, template + '/page_list.html', context)

# Get an array of all parent instances of a Page model
def get_list_of_parents(page, list=None):

    current_page = page 
    parent_list = []    

    if list is not None:
        parent_list = list

    parent_list.append(current_page)

    if current_page.parent is not None:
        parent_list = get_list_of_parents(current_page.parent, parent_list)
    else:
        # if this is the last parent page, reverse the order of list to display list in form of parent path to child
        parent_list.reverse()

    return parent_list
</code></pre>

<p>Take advantage of helper functions in itertools and do your sorting with a simple and highly efficient one-line comparison function.</p>

<pre><code>&gt;&gt;&gt; import pprint
&gt;&gt;&gt; from itertools import dropwhile, izip_longest
&gt;&gt;&gt; pages = [['CCC', 'AAA'],
...          ['ZZZ', 'BBB'],
...          ['CCC'],
...          ['CCC', 'AAA', 'DDD'],
...          ['CCC', 'AAA', 'DDD', 'EEE'],
...          ['CCC', 'AAA', 'KKK'],
...          ['ZZZ']]
&gt;&gt;&gt; pprint.pprint(sorted(pages, cmp=lambda a, b: cmp(*next(dropwhile(lambda x: not cmp(x[0], x[1]), izip_longest(a, b))))))
[['CCC'],
 ['CCC', 'AAA'],
 ['CCC', 'AAA', 'DDD'],
 ['CCC', 'AAA', 'DDD', 'EEE'],
 ['CCC', 'AAA', 'KKK'],
 ['ZZZ'],
 ['ZZZ', 'BBB']]
&gt;&gt;&gt;
</code></pre>

<p>This works not only for strings, but also Page instances, as long as they are comparable, i.e. that you have defined your <code>__cmp__</code> method in your Page class.</p>

python django sorting order nested-lists