Sort of number

Discussion in 'AutoCAD' started by Adesu, Nov 25, 2004.

  1. Adesu

    Adesu Guest

    I want sort of number

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))
    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    to (1 1 2 3 3 3 4 4 4 5 5 6 7)

    And for string I can do it,like this

    _$ (setq lis1 (list "B" "C" "R" "W" "D" "B" "G" "A" "C"))
    ("B" "C" "R" "W" "D" "B" "G" "A" "C")

    _$ (setq sor (acad_strlsort lis1))
    ("A" "B" "B" "C" "C" "D" "G" "R" "W")
     
    Adesu, Nov 25, 2004
    #1
  2. Adesu

    Jeff Mishler Guest

    Here's one way:
    (setq nthList (vl-sort-i lis '<))
    (foreach x nthlist
    (setq newList (cons (nth x lis) newlist))
    )
    (setq newlist (reverse newlist))
     
    Jeff Mishler, Nov 25, 2004
    #2
  3. Adesu

    Adesu Guest

    Hi Jeff, yes you are perfect,that object I am looking for,thanks a lot
     
    Adesu, Nov 25, 2004
    #3
  4. Adesu

    Adesu Guest

    Hi Jeff, I am not understand

    (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    (setq nthList (vl-sort-i lis '<))

    (11 5 12 9 8 1 7 4 0 10 3 2 6) +++>> why display "11" "12" "9" "8" "0"
    "10",where it find?

    (foreach x nthlist (setq newList (cons (nth x lis) newlist)))

    (7 6 5 5 4 4 4 3 3 3 2 1 1)

    (setq newlist (reverse newlist))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)
     
    Adesu, Nov 25, 2004
    #4
  5. Adesu

    Jeff Mishler Guest

    Ade,
    The (vl-sort-i) returns the sorted INDEX numbers of the elements....that's
    why I next used the (nth) function to obtain the actual elemnt of the list.
     
    Jeff Mishler, Nov 25, 2004
    #5
  6. Adesu

    Alaspher Guest

    I use:
    Code:
    (defun pl:sort (func lst)
    (mapcar (function (lambda (x) (nth x lst))) (vl-sort-i lst func))
    )
    For your case, it'll be so:
    Code:
    (pl:sort (function <) lis)
     
    Alaspher, Nov 25, 2004
    #6
  7. Adesu

    Adesu Guest

    Yes Alaspher ,thanks a lot too.

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))
    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    $ (mapcar (function (lambda (x) (nth x lis))) (vl-sort-i lis '<))
    (1 1 2 3 3 3 4 4 4 5 5 6 7)
     
    Adesu, Nov 25, 2004
    #7
  8. Adesu

    Adesu Guest

    Thanks to Jeff & Alanpher,here is format to sort of number

    ; MODEL 1

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    _$ (setq nthList (vl-sort-i lis '<))

    (11 5 12 9 8 1 7 4 0 10 3 2 6)

    _$ (foreach x nthlist (setq newList (cons (nth x lis) newlist)))

    (7 6 5 5 4 4 4 3 3 3 2 1 1)

    _$ (setq newlist (reverse newlist))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)

    ; MODEL 2

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    _$ (mapcar (function (lambda (x) (nth x lis))) (vl-sort-i lis '<))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)
     
    Adesu, Nov 25, 2004
    #8
  9. Tony Tanzillo:
    If you search this newsgroup, you'll find a much
    more powerful sorting function along with a good
    discussion on why (vl-sort) can be very dangerous.
    For that reason, I suggest you replace the built-in
    vl-sort with this:

    (defun vl-sort (lst func)
    (mapcar
    '(lambda (x) (nth x lst))
    (vl-sort-i lst func)
    )
    )

    This will ensure that (vl-sort) does not remove
    elements that it sees as equal.

    Comando: (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))
    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    Comando: (vl-sort lis '<)
    (1 1 2 3 3 3 4 4 4 5 5 6 7)

    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
     
    Marc'Antonio Alessi, Nov 25, 2004
    #9
  10. Adesu

    Adesu Guest

    Hi Marc', good idea from you,thanks a lot ,now I got more option to choose
    one it.

    ; MODEL 1

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    _$ (setq nthList (vl-sort-i lis '<))

    (11 5 12 9 8 1 7 4 0 10 3 2 6)

    _$ (foreach x nthlist (setq newList (cons (nth x lis) newlist)))

    (7 6 5 5 4 4 4 3 3 3 2 1 1)

    _$ (setq newlist (reverse newlist))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)

    ; MODEL 2

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    _$ (mapcar (function (lambda (x) (nth x lis))) (vl-sort-i lis '<))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)

    ; MODEL 3

    _$ (mapcar '(lambda (x) (nth x lis))(vl-sort-i lis '<))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)
     
    Adesu, Nov 26, 2004
    #10
  11. I Adesu,

    despite I prefer T.T. approach:

    (defun vl-sort (lst func)
    (mapcar
    '(lambda (x) (nth x lst))
    (vl-sort-i lst func)
    )
    )

    (defun sort-foreach (lst / newlist)
    (foreach x (vl-sort-i lst '<)
    (setq newList (cons (nth x lst) newlist))
    )
    (reverse newlist)
    )

    SORT-FOREACH > 100000 iterations: 10.59 secs.

    VL-SORT > 100000 iterations: 9.91 secs.


    Cheers.


    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
     
    Marc'Antonio Alessi, Nov 26, 2004
    #11
  12. Adesu

    Adesu Guest

    Hi Marc ,how to find
     
    Adesu, Nov 26, 2004
    #12
  13. ; Uhden's timer
    (defun timerU (fun n / start stop)
    (gc)(gc)
    (setq start (getvar "date"))
    (repeat n (eval fun))
    (setq stop (getvar "date"))
    (terpri)
    (princ (car fun))
    (princ
    (strcat
    ;"Elapsed time for " (itoa n) " iterations: " ;originale
    " > " (itoa n) " iterations: "
    (rtos (* 86400.0 (- stop start)) 2 2) " secs.\n"
    )
    )
    (gc)(gc)
    (eval fun)
    )


    (timerU '(sort-foreach '(4 3 6 5 4 1 7 4 3 3 5 1 2)) 100000))

    (timerU '(vl-sort '(4 3 6 5 4 1 7 4 3 3 5 1 2) '<) 100000)

    ------------------------------------------------------------

    ;Bench - Copyright © 1989-2002 by R. Robert Bell.

    (defun Timer_ori (Func Args Count / FuncArgs Start)
    (gc)
    (setq Start (getvar "Millisecs"))
    (while (< (getvar "Millisecs") (+ Start 1000)))
    (if (= (type Args) 'STR)
    (progn
    (setq
    FuncArgs (read (strcat "(" Func " " Args ")"))
    Start (getvar "Millisecs")
    )
    (repeat Count (eval FuncArgs))
    (- (getvar "Millisecs") Start)
    )
    (progn
    (setq Start (getvar "Millisecs"))
    (repeat Count (apply Func Args))
    (- (getvar "Millisecs") Start)
    )
    )
    )


    (defun Timer (Func Args Count / FuncArgs Start)
    (gc)
    (setq Start (getvar "CPUTICKS"))
    (while (< (getvar "CPUTICKS") (+ Start 1000)))
    (if (= (type Args) 'STR)
    (progn
    (setq
    FuncArgs (read (strcat "(" Func " " Args ")"))
    Start (getvar "CPUTICKS")
    )
    (repeat Count (eval FuncArgs))
    (- (getvar "CPUTICKS") Start)
    )
    (progn
    (setq Start (getvar "CPUTICKS"))
    (repeat Count (apply Func Args))
    (- (getvar "CPUTICKS") Start)
    )
    )
    )

    (defun Bench_ori (Funcs Args Count / Time)
    (foreach Func Funcs
    (terpri)
    (princ Func)
    (princ (strcat "\nElapsed: " (itoa (setq Time (timer Func Args
    Count)))))
    (princ (strcat "\nAverage: " (rtos (/ (float Time) Count) 2 4)))
    (terpri)
    )
    (princ)
    )
    (defun Bench (Funcs Args Count / Time)
    (foreach Func Funcs
    (terpri)
    (princ Func)
    (princ (strcat "\nElapsed: " (rtos (setq Time (timer Func Args Count)) 2
    4)))
    (princ (strcat "\nAverage: " (rtos (/ Time Count) 2 4)))
    (terpri)
    )
    (princ)
    )
    ; (princ "Bench - Copyright © 1989-2002 by R. Robert Bell.")
    ; (princ (strcat
    ; "\nUsage: (Bench '(func1 func2) '(arg1 arg2) 1000) for \"hard\"
    arguments,"
    ; "\nor (Bench '(\"func1\" \"func2\") \"arg1 arg2\" 1000) for actual bound
    variables."
    ;) )

    ;(Bench '(func1 func2) '(arg1 arg2) 1000)
    ;(Bench '("func1" "func2") "arg1 arg2" 1000)


    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
     
    Marc'Antonio Alessi, Nov 26, 2004
    #13
  14. Adesu

    Adesu Guest

    _$ (setq lis (list 4 3 6 5 4 1 7 4 3 3 5 1 2))

    (4 3 6 5 4 1 7 4 3 3 5 1 2)

    _$ (setq nthList (vl-sort-i lis '<))

    (11 5 12 9 8 1 7 4 0 10 3 2 6)

    _$ (foreach x nthlist (setq newList (cons (nth x lis) newlist)))

    (7 6 5 5 4 4 4 3 3 3 2 1 1)

    _$ (setq newlist (reverse newlist))

    (1 1 2 3 3 3 4 4 4 5 5 6 7)

    _$ (defun timerU (fun n / start stop)
    (gc)(gc)
    (setq start (getvar "date"))
    (repeat n (eval fun))
    (setq stop (getvar "date"))
    (terpri)
    (princ (car fun))
    (princ
    (strcat
    ;"Elapsed time for " (itoa n) " iterations: " ;originale
    " > " (itoa n) " iterations: "
    (rtos (* 86400.0 (- stop start)) 2 2) " secs.\n"
    )
    )
    (gc)(gc)
    (eval fun)
    )
    TIMERU
    _$ (timerU '(sort-foreach newlist) 100000)
    ; error: no function definition: SORT-FOREACH
    _$ (timerU '(vl-sort newlist '<) 100000)

    VL-SORT > 100000 iterations: 46.09 secs.

    (1 2 3 4 5 6 7)

    Hi Marc, yes .....I now understand about timer,and thank for your and
    special thanks for " Jhon Uhden", "R. Robert Bell."for share your timer
    code.
     
    Adesu, Nov 26, 2004
    #14
  15. _$ (timerU '(sort-foreach newlist) 100000)
    you must load the two functions also:

    (defun vl-sort (lst func)
    (mapcar
    '(lambda (x) (nth x lst))
    (vl-sort-i lst func)
    )
    )

    (defun sort-foreach (lst / newlist)
    (foreach x (vl-sort-i lst '<)
    (setq newList (cons (nth x lst) newlist))
    )
    (reverse newlist)
    )
     
    Marc'Antonio Alessi, Nov 26, 2004
    #15
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.