Author Topic: More sort help  (Read 1856 times)

0 Members and 1 Guest are viewing this topic.

CincyJeff

  • Newt
  • Posts: 89
More sort help
« on: December 08, 2015, 03:40:56 PM »
About a month ago I needed some sorting help and ronjonp came to me rescue. I though I could just modify the previous code for my new task but I can't get it to work. I have a list as follows:
((((58097.8 68160.0) (58279.0 67834.1)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58793.2 68703.3) (58974.4 68377.4)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68426.5) (58279.0 68257.3)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58489.9 68305.8) (58671.1 67979.9)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68861.5) (58279.0 68535.6)) (#<VLA-OBJECT #<VLA-OBJECT)))
I want to sort each element first by the smallest x-value shown in red and if the x-values match, then sort by largest y-value shown in green. The final list should look like:
((((58097.8 68861.5) (58279.0 68535.6)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68426.5) (58279.0 68257.3)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68160.0) (58279.0 67834.1)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58489.9 68305.6) (58671.1 67979.9)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58793.2 68703.3) (58974.4 68377.4)) (#<VLA-OBJECT #<VLA-OBJECT)))
I tried the following code but without success:
Code - Auto/Visual Lisp: [Select]
  1. (while (car ss)
  2.   ;; Get X value
  3.   (setq x (caaar (car ss)))
  4.   ;; Grab all other list entries with same X within a fuzz of 12 while sorting by largest Y value
  5.   (setq tmp (vl-sort (vl-remove-if-not
  6.                    '(lambda (z) (equal x (caaar z) 0.1))
  7.                    ss
  8.                  ) ;_ end of vl-remove-if-not
  9.                  '(lambda (a b)
  10.                     (> (cadaar a)
  11.                         (cadaar b)
  12.                     ) ;_ end of >
  13.                   ) ;_ end of lambda
  14.         ) ;_ end of vl-sort
  15.   ) ;_ end of setq
  16.   ;; Put items in list 'out'
  17.   (setq out (cons tmp out))
  18.   ;; Remove from list 'ss' we're iterating
  19.   (foreach tm tmp (setq ss (vl-remove tm ss)))
  20.  ) ;_ end of while
  21.  (setq sortgrp:l (reverse out))
Can anybody help?

CincyJeff

  • Newt
  • Posts: 89
Re: More sort help
« Reply #1 on: December 08, 2015, 03:46:50 PM »
I forgot to mention that I can sort for the x-values with:
Code - Auto/Visual Lisp: [Select]
  1. (setq ss (vl-sort sortgrp:l
  2.                   '(lambda (x1 x2)
  3.                      (< (caaar x1)
  4.                           (caaar x2)
  5.                      ) ;_ end of >
  6.                    ) ;_ end of lambda
  7.          ) ;_ end of vl-sort
  8. ) ;_ end of setq


EDIT: Added code tag. - John
« Last Edit: December 08, 2015, 03:47:55 PM by John Kaul (Se7en) »

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: More sort help
« Reply #2 on: December 08, 2015, 04:31:18 PM »
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort l '(lambda ( a b ) (if (= (caaar a) (caaar b)) (> (cadaar a) (cadaar b)) (< (caaar a) (caaar b)))))
  2.  

HTH, M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

CincyJeff

  • Newt
  • Posts: 89
Re: More sort help
« Reply #3 on: December 09, 2015, 07:39:58 AM »
I'm still getting the same result I was getting with the posted code:
(((58097.8 68861.5) (58279.0 68535.6)) ((58097.8 68160.0) (58279.0 67834.1)) ((58097.8 68426.5) (58279.0 68257.3))
The y-values are not in descending order. Any ideas?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: More sort help
« Reply #4 on: December 09, 2015, 08:25:43 AM »
Any ideas?
I think you have changed the format of the list. In your 1st post there are 4 parenthesis in front of the first number. In your last post there are only 3. Ribarm's suggestions works with the original list format.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: More sort help
« Reply #5 on: December 09, 2015, 08:29:31 AM »
Worked for me.

Code: [Select]
(setq lst '((((58097.8 68160.0) (58279.0 67834.1)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58793.2 68703.3) (58974.4 68377.4)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68426.5) (58279.0 68257.3)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58489.9 68305.8) (58671.1 67979.9)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68861.5) (58279.0 68535.6)) (#<VLA-OBJECT #<VLA-OBJECT))))

(setq *fuzz 1.0e-6)
(defun mysort (l)
(vl-sort l '(lambda ( a b ) (if (equal (caaar a) (caaar b) *fuzz) (> (cadaar a) (cadaar b)) (< (caaar a) (caaar b)))))
)
 (mysort lst)
_1_$
((((58097.8 68861.5) (58279.0 68535.6)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68426.5) (58279.0 68257.3)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58097.8 68160.0) (58279.0 67834.1)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58489.9 68305.8) (58671.1 67979.9)) (#<VLA-OBJECT #<VLA-OBJECT)) (((58793.2 68703.3) (58974.4 68377.4)) (#<VLA-OBJECT #<VLA-OBJECT)))
_$
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CincyJeff

  • Newt
  • Posts: 89
Re: More sort help
« Reply #6 on: December 09, 2015, 10:35:34 AM »
roy_043,
I was just posting the result of the list that shared the same x-value to show that the y-values were not sorted the way I wanted. CAB fixed that.

CAB,
The fuzz factor did the trick. Thanks so much. You guys are great!