(defun foo (ele lst)
(reverse
(cdr
(vl-sort
(cons ele lst)
'(lambda (x1 x2) (< (car x1) (car x2)))
)
)
)
)
(setq ScoresList '((10000 "Han solo" 5) (8000 "Luke" 3) (5000 "Vader" 3) (5000 "Luke" 2) (4000 "Vader" 2) (2000 "Luke-1" 1)))
_$ (InsertScores ScoresList '(6000 "Obiwan" 5))
((10000 "Han solo" 5) (8000 "Luke" 3) (6000 "Obiwan" 5) (5000 "Luke" 2) (5000 "Vader" 3) (4000 "Vader" 2))
Same code though ... it does seem to be the shortest version (though not necessarily the "quickest" ;) ).
Code removed ... See Gile and irneb
_$ (score1 '((10 "Mara" 2) (8 "Peter" 3) (8 "Peter" 3) (8 "Peter" 3) (1 "Lola" 8)) '(8 "Arthur" 4))
((10 "Mara" 2) (8 "Arthur" 4) (8 "Peter" 3) (8 "Peter" 3) (8 "Peter" 3))
@Rain CodeBenchmarking .... done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO ITEM SCORESLIST) 8192 1171 1171 1.12
(INSERTSCORES ITEM SCORESLIST) 8192 1201 1201 1.09
(IB:INSERTITEM ITEM SCORESLIST) 8192 1218 1218 1.08
(INSERTITEM ITEM SCORESLIST) 8192 1311 1311 1.00
--------------------------------------------------------------------------------
_$ (QuickBench (mapcar '(lambda (f) (list f 'item 'ScoresList)) '(InsertScores foo insertitem IB:InsertItem)))
Benchmarking .... done for 8192 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(FOO ITEM SCORESLIST) 8192 1155 1155 1.17
(INSERTSCORES ITEM SCORESLIST) 8192 1201 1201 1.13
(IB:INSERTITEM ITEM SCORESLIST) 8192 1232 1232 1.10
(INSERTITEM ITEM SCORESLIST) 8192 1356 1356 1.00
--------------------------------------------------------------------------------
gile and my 1st one is actually the exact same code, so the differences are due to the benchmarking. But no matter how many times I perform this, the idea of using mapcar instead of reverse never wins out.What about level? Does it make a difference in the sorted list?I'm guessing it would if the scores are the same, then a lower level would outrank a higher one. In which case the comparison function could become:
@Rain Code
What happens when a new score is equal to another in the list? It will goes in front or after the existing one?
What about level? Does it make a difference in the sorted list?
Command: (vl-sort '(1 2 3 3 5 7 7 7 9 0 0 0) '(lambda (a b) (< a b)))
(0 1 2 3 5 7 9)
Just curious: When does vl-sort omit duplicates? From my tests it doesn't do so:omit only when list is (list 0 1 2 3 4) but ok when list is list of dotted pairs (list (1 . "a") (2 . "b"))Code - Auto/Visual Lisp: [Select]
_$ (InsertScores '(8 "Arthur" 4) '((10 "Mara" 2) (8 "Peter" 3) (8 "Peter" 3) (8 "Peter" 3) (1 "Lola" 8))) ((10 "Mara" 2) (8 "Peter" 3) (8 "Peter" 3) (8 "Peter" 3) (8 "Arthur" 4))
Am I missing something? That test is done on 64bit ACad Vanilla 2012, is there a difference with other versions or clones?
Command: (vl-sort (list (list 0 1) (list 1 1) (list 1 1) (list 2 1)) '(lambda
(a b) (< (car a) (car b))))
((0 1) (1 1) (1 1) (2 1))
Command: (setq p1 (getpoint))
(60.3202 23.1086 0.0)
Command: (setq p2 (getpoint))
(76.2727 30.3611 0.0)
Command: (setq p3 (getpoint))
(90.5159 25.5261 0.0)
Command: (vl-sort (list p1 p2 p2 p1 p1 p3 p3 p3) '(lambda (a b) (< (car a) (car
b))))
((60.3202 23.1086 0.0) (76.2727 30.3611 0.0) (90.5159 25.5261 0.0))
Command: (setq p1 (list 0 1))
(0 1)
Command: (setq p2 (list 1 1))
(1 1)
Command: (setq p3 (list 2 1))
(2 1)
Command: (vl-sort (list p1 p2 p2 p1 p1 p3 p3 p3) '(lambda (a b) (< (car a) (car
b))))
((0 1) (1 1) (2 1))