If Random is not a list of compound terms, use Key = 0.
If Random is a list of compound terms, then the sort will be according to the Keyth argument of the first element of the lists. If Key = 0, then the sort is on the entire terms.
In all cases, Order specifies whether the list is sorted into ascending (<, =<) or descending (>, >=) order and whether duplicates are to be retained (=<, >=) or eliminated (<, >). The way to remember the Order argument is that it is the relation which holds between adjacent elements in the result.
The sort is done according to the standard ordering of terms. See compare/3 for the definition of this standard ordering.
Success: sort(0,<,[3,1,6,7,2],S). (gives S=[1,2,3,6,7]). sort(0,>,[q,1,3,a,e,N],S). (gives N=_g78,S=[q,e,a,3,1,_g78]). sort(0,=<,[1,3,2,3,4,1],S). (gives S=[1,1,2,3,3,4]). sort(2,<,[f(1,3),h(2,1)],S). (gives S=[h(2,1),f(1,3)]). sort(1,<,[f(1,3),h(2,1)],S). (gives S=[f(1,3),h(2,1)]). sort([2,1],=<,[f(3,a(2)),f(1,a(1)),f(0,a(3)),f(1,a(4))],S). (gives S=[f(1,a(1)),f(3,a(2)),f(0,a(3)),f(1,a(4))]). Fail: sort(0,<,[2,1,3,4],[2,1,3,4]). Error: sort(1,<,[f(1),f(3),5],S). (Error 5). sort(1.0,<,[f(1),f(3),f(5)],S). (Error 5). sort(2,<,[f(1,2),g(3,a),f(5)],S). (Error 6).