Sorting list of lists by their first element in scheme
<p>I'm working on sorting a list of lists by their first element for example<br>
(sort (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 1))))</p>

<p>expected output => ('(1 1) '(2 1 6 7) '(4 3 1 2 4 5))</p>

<p>The algorithm I used is bubble sort. And I modified it to deal with lists. However, the code doesn't compile. The error is </p>

<pre><code>mcar: contract violation
  expected: mpair?
  given: 4
</code></pre>

<p>Can someone correct my code and explain it. Thank you</p>

<pre><code> (define (bubble L)
        (if (null? (cdr L))   
            L    
            (if (&lt; (car (car L)) (car (cadr L)))   
                (list (car L)
                      (bubble (car (cdr L))))   
                (list (cadr L)
                      (bubble (cons (car (car L)) (car (cddr L))))))))

    (define (bubble-sort N L)    
        (cond ((= N 1) (bubble L))   
              (else
               (bubble-sort (- N 1) (bubble L)))))

    (define (bubble-set-up L) 
        (bubble-sort (length L) L))


    (define t3 (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 2 3) '(1 1)))
    (bubble-set-up t3)
</code></pre>