0

I have a list made out of lists (preorder-mst) like this:

((arc graph-id vertex-a vertex-b weight) (arc graph-id vertex-a vertex-b weight) ...)

What I wanted to do was to sort the sublists by weight and, if 2 weights were equal, by vertex-b.

I tried to call a function to sort the elements

(sort preorder-mst 'compare-string-number)

(defun compare-string-number (firstLIST secondLIST)
    (if (eql (fifth firstLIST) (fifth secondLIST))
        (if (string-lessp (fourth firstLIST) (fourth secondLIST))
            (fourth firstLIST)
            (fourth secondLIST))
        (when T
            (if (< (fifth firstLIST) (fifth secondLIST))
                (fifth firstLIST)
                (fifth secondLIST)))))

It returns the correct value but doesn't sort them correctly. Any idea what is wrong with it?

My (undesired) output:

((ARC GRAFO_TEST_1 C I 2) (ARC GRAFO_TEST_1 G H 1) (ARC GRAFO_TEST_1 NIL A 0) (ARC GRAFO_TEST_1 B C 8) (ARC GRAFO_TEST_1 A B 4))

1 Answer 1

2

Predicates used in sort work like this: they take two arguments and return true or false. If the predicate returns true then the first argument is considered in priority while sorting, else the second. To learn more about how sort works and the predicate it requires see here.

Remember that almost all values are "truthy" in lisp, while the empty list or nil are false. See in the hyperspec the term generalized boolean.

Your function almost always returns true, since it returns the elements you want to compare and they have truthy values. To fix that you have to return the comparisons themselves:

(defun graph-sort-p (firstLIST secondLIST)
  (if (= (fifth firstLIST) (fifth secondLIST))
      (string-lessp (fourth firstLIST) (fourth secondLIST))
      (< (fifth firstLIST) (fifth secondLIST))))

(sort my-list  #'graph-sort-p)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.