Author Topic: Sort List by List  (Read 7639 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #15 on: February 01, 2010, 01:10:41 PM »
Wow! A lot of interest, nice one everyone - I love that recursive solution Evgeniy!

Maybe I missed something but what's the design behavior when the length of the list to be sorted is greater than that of the sort index?

Michael,

The original request that spurred this on was to write a bunch of attribute tags to Excel in a particular order, that order was denoted by a 'reference list'.

But of course, if any tags are surplus to the list, these must be included too.

The attribute tag list would look something like:
Code: [Select]
((Category_Tag (Type_Tag . Value_Tag)) (Category_Tag (Type_Tag . Value_Tag)) ... )

Where the list would be sorted by referencing the "Category Tag" and the "Type_Tag" and "Value_Tag" are written under each category.

So, that is what spurred this on...  :-)

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #16 on: February 01, 2010, 01:11:27 PM »
OK, time to see who's is the fastest. :evil:

Not me!  Too many loops in mine I think...  :|

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sort List by List
« Reply #17 on: February 01, 2010, 01:16:26 PM »
Ok ... as far as genericizing this .... is the intent to use the sort index associatively or positionally?

That is, given a raw list of '("C" "B" "A") should it sort using a sort index of '(2 1 3) as '("B" "C" "A") <positional sort> or since there's no association as it was, '("C" "B" "A")??
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #18 on: February 01, 2010, 01:31:31 PM »
Ok ... as far as genericizing this .... is the intent to use the sort index associatively or positionally?

That is, given a raw list of '("C" "B" "A") should it sort using a sort index of '(2 1 3) as '("B" "C" "A") <positional sort> or since there's no association as it was, '("C" "B" "A")??

Good point. In my original code, I sought out the position of the category in the 'reference list' and then moved the items accordingly, but this would have to be changed for whichever item was to be sort by.

So to make the code generic, I suppose a sort index would be the best option, becoming something like:

Code: [Select]
(defun SortByIndex (lst ind)
  (mapcar
    (function
      (lambda (x) (nth x lst))) ind))

Or allowing for missing elements...

Code: [Select]
(defun SortByIndex (lst ind / new)
  (setq new
    (mapcar
      (function
        (lambda (x) (nth x lst))) ind))

  (append new (vl-remove-if
                (function (lambda (x) (vl-position x new))) lst)))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Sort List by List
« Reply #19 on: February 01, 2010, 01:45:14 PM »

(SortByList '("A" "B" "D" "C" "D") '("D" "A" "P" "B"));;=> ("D" "A" "B" "C")
(ReorderList '("A" "B" "D" "C" "D") '("D" "A" "P" "B"));;=> ("D" "A" "B" "C")
(sort '("A" "B" "D" "C" "D") '("D" "A" "P" "B") );;=> ("D" "A" "B" "C")
(sortlist '("A" "B" "D" "C" "D") '("D" "A" "P" "B") nil);;=> ("D" "D" "A" "B" "C")
(ReorderList '("A" "B" "D" "C" "D") '("D" "A" "P" "B"));;=> ("A" "B" "D" "C" "D")


corrected:
Code: [Select]
(defun rem-1 (a b)
 (cond ((not b) nil)
       ((= a (car b)) (cdr b))
       ((cons (car b) (rem-1 a (cdr b))))
 )
)
(defun sort (a b)
 (cond ((not b) a)
       ((member (car b) a) (cons (car b) (sort (rem-1 (car b) a) (cdr b))))
       ((sort a (cdr b)))
 )
)

(sort '("A" "B" "D" "C" "D") '("D" "D" "A" "P"))
;;=> ("D" "D" "A" "B" "C")
« Last Edit: February 01, 2010, 01:53:39 PM by ElpanovEvgeniy »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sort List by List
« Reply #20 on: February 01, 2010, 01:57:49 PM »
Good point. In my original code, I sought out the position of the category in the 'reference list' and then moved the items accordingly, but this would have to be changed for whichever item was to be sort by.

One of the reasons I asked is that the posts seemed to be using an associative sort, kinda dicey when using strings -- is the intent to honor case etc? What happens if the data is numerical, nested lists, or worse, objects rather than strings? To continue, it appeared to me the intent that was positional, as said approach would work using the original data supplied. Please correct me if my assumptions are incorrect.

So to make the code generic, I suppose a sort index would be the best option, becoming something like:

Code: [Select]
(defun SortByIndex (lst ind)
  (mapcar
    (function
      (lambda (x) (nth x lst))) ind))

Or allowing for missing elements...

Code: [Select]
(defun SortByIndex (lst ind / new)
  (setq new
    (mapcar
      (function
        (lambda (x) (nth x lst))) ind))
  (append new (vl-remove-if
                (function (lambda (x) (vl-position x new))) lst)))

?? I don't see how either of those functions perform an actual a sort, what am I missing?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #21 on: February 01, 2010, 02:05:47 PM »
?? I don't see how either of those functions perform an actual a sort, what am I missing?

The functions are designed so that given an index list, the items are sorted accordingly, hence;

Code: [Select]
(sortbyIndex '("A" "B" "C" "D") '(2 0 1 3))
("C" "A" "B" "D")

But perhaps its me who's losing it  - highly likely

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sort List by List
« Reply #22 on: February 01, 2010, 02:09:16 PM »
The functions are designed so that given an index list, the items are sorted accordingly, hence;

Code: [Select]
(sortbyIndex '("A" "B" "C" "D") '(2 0 1 3))
("C" "A" "B" "D")

A positional sort using that data should return ("B" "C" "A" "D"), no?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #23 on: February 01, 2010, 02:14:06 PM »
The functions are designed so that given an index list, the items are sorted accordingly, hence;

Code: [Select]
(sortbyIndex '("A" "B" "C" "D") '(2 0 1 3))
("C" "A" "B" "D")

A positional sort using that data should return ("B" "C" "A" "D"), no?

We are still using Zero-based index right?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sort List by List
« Reply #24 on: February 01, 2010, 02:21:56 PM »
yep
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: Sort List by List
« Reply #25 on: February 01, 2010, 02:26:17 PM »
yep

My mistake - you are right (as usual)  :oops:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Sort List by List
« Reply #26 on: February 01, 2010, 03:33:14 PM »
My mistake - you are right (as usual)  :oops:

Actually I'm not right.

It should be base indifferent, that is, I believe the behavior of the function should be such that this statement:

(_SortByKeys '("A" "B" "C") '(99 55 27))

would return:

("C" "B" "A")

But my expectation of said behavior (use of the sort keys) may deviate from the consensus here.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst