flopo Posted February 5, 2013 Posted February 5, 2013 Hi guys, Anybody can explain me what's happening here? (setq lst (list 1 5 7 20 4 44 123 15 54 32 7 20 4 44 76 12)) (vl-sort lst '(lambda (a b) ( < (/ a b) 3))) (setq lst (list 1 5 7 20 4 44 123 15 54 32 7 20 4 44 76 12)) (vl-sort lst '(lambda (a b) ( > (/ a b) 3))) I try to understand how vl-sort is working... Quote
uli Posted February 5, 2013 Posted February 5, 2013 The result is (44 123 54 32 44 76 5 20 4 15 7 20 4 12 1) - i can't see any order in this list....??? Quote
MSasu Posted February 5, 2013 Posted February 5, 2013 I really don't understand your sorting criteria. You try to sort the numbers as well the result of division between two adjacent items to be smaller than 3?!? The result of such sorting will be influenced by the initial order of the numbers, so is totally unreliable: Case 1: (setq lst (list 1 5 7 20 4 44 123 15 54 32 7 20 4 44 76 12)) (vl-sort lst '(lambda (a b) ( > (/ a b) 3))) [color=magenta];Result: (44 123 54 32 44 76 5 20 4 15 7 20 4 12 1)[/color] Case 2: (setq lst (list 7 20 4 44 76 7 20 4 44 12 123 15 54 1 5 32)) (vl-sort lst '(lambda (a b) ( > (/ a b) 3))) [color=magenta];Result: (76 20 44 123 54 4 7 20 4 12 15 32 5 1)[/color] Quote
togores Posted February 5, 2013 Posted February 5, 2013 To examine what goes on in vl-sort or vl-sort-i you can see my post in this same forum: http://www.cadtutor.net/forum/showthread.php?77049-LAMBDA-expressions-used-as-comparison-operators-for-sorting It shows how you can print to the Console the values compared by the sorting functions in each iteration. Quote
Lee Mac Posted February 5, 2013 Posted February 5, 2013 Anybody can explain me what's happening here? vl-sort will re-order items in a given list based upon the return of the supplied comparison function for every pair of items tested. If the comparison function returns T for the two arguments, this indicates that the first argument precedes the second argument in the sorted list, and so by comparing all pairs, vl-sort will alter list order accordingly. There are many different algorithms which use this comparison sorting technique (Quick Sort, Merge Sort, Bubble Sort, to name a few); the particular algorithm implemented by the vl-sort function is not documented, but I would guess that it likely uses a Quick Sort algorithm. You may find this thread of particular interest when understanding the inner-workings of a sorting algorithm. What result are you looking to obtain from your function? Quote
flopo Posted February 5, 2013 Author Posted February 5, 2013 In fact, i reached this situation by mistake... and this is why i was trying to understand the algorithm inside vl-sort...thanks, Lee! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.