Author Topic: Sort Strings in Dotted Pairs  (Read 1861 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Sort Strings in Dotted Pairs
« on: June 30, 2009, 12:41:32 PM »
I'm probably missing something easy here, but I can't think how to approach this.

I need to sort a list of dotted pairs into alphabetical order, hence:

Code: [Select]
((lee . 12) (can't . 45) (work . 43) (this . 93) (out . 83))

Returns:

Code: [Select]
((can't . 45) (lee . 12) (out . 83) (this . 93) (work . 43))

I was thinking something like:

Code: [Select]
(vl-sort '((lee . 12) (can't . 45) (work . 43) (this . 93) (out . 83)) (some function using acad_strlsort....

But I am lost on this one for the moment  :oops:

Any help is welcomed and appreciated.

Lee


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Sort Strings in Dotted Pairs
« Reply #1 on: June 30, 2009, 01:09:52 PM »
First you need to make them strings, or if they are variables that holds strings......

Code: [Select]
Command: (setq lst '(("lee" . 12) ("can\'t" . 45) ("work" . 43) ("this" . 93)
("out" . 83)))
(("lee" . 12) ("can't" . 45) ("work" . 43) ("this" . 93) ("out" . 83))

Command: (vl-sort lst '(lambda (a b) (< (car a) (car b))))
(("can't" . 45) ("lee" . 12) ("out" . 83) ("this" . 93) ("work" . 43))
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Sort Strings in Dotted Pairs
« Reply #2 on: June 30, 2009, 01:25:09 PM »
Sorry, yes, they were meant to be strings - not sure how I missed the quotes...  :ugly:

Thanks Tim, I didn't know that "<" ">" could work for strings also - I'm learning.  :angel:

Cheers,

Lee


David Bethel

  • Swamp Rat
  • Posts: 656
Re: Sort Strings in Dotted Pairs
« Reply #3 on: July 01, 2009, 10:06:55 AM »
I think this was Steve Johnson's.  It may have been Jon Fleming's.  Anyway at least 1999 or before

Code: [Select]
;;******************************************************
;; ********************* SORTDPLST *********************
;;******************************************************
; (SORTDPLST <List_of_lists>)
; Sorts a list of lists (or dotted pairs) by the first element
; in ascending order. The first elements may be either all numeric or
; all strings.

(defun SortDpLst (dpl / len ctr start mcg rlen minimum)

(defun minimum (lst)
  (if (= (type (car lst)) 'STR)
    (car (acad_strlsort lst))
    (apply 'min lst)
  )
)

  (setq len (length dpl)
        ctr 0
        start nil)
  (cond ((> len 2)
    (setq mcg (minimum (mapcar 'car dpl))) ; get smallest value
    (setq mcg (assoc mcg dpl))
    (setq rlen (- len (length (member mcg dpl)))) ; get position in list
    (while (< ctr rlen)  ; remove minimum from list
      (setq start (cons (nth ctr dpl) start))
      (setq ctr (1+ ctr))
    )
    (setq dpl (cons mcg (sortdplst (append (reverse start) ; reconstruct list
                                   (cdr (member mcg dpl))))))
  )
  ((= len 2)
    (if (/= (minimum (mapcar 'car dpl)) (caar dpl))
      (setq dpl (reverse dpl))
    )
  ))
  dpl
)
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Sort Strings in Dotted Pairs
« Reply #4 on: July 01, 2009, 12:14:15 PM »
Very nice David - thanks  :-)