Author Topic: Help with a vl-sort  (Read 2016 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
Help with a vl-sort
« on: October 31, 2012, 07:42:53 PM »
I'm trying to sort a list as follows:

(setq rlst (list '("1.02" "A" "B" "DEF" "id10t") '("2.06" "Q" "R" "STU" "pid") '("10.05" "L" "M" "N-O-PEE" "id10ts") '("39.22" "H" "I" "MOM" "LovE") '("122.22" "X" "Y" "ZED" "fail")))

I'm needing is sort as follows:
(("1.02" "A" "B" "DEF" "id10t")("2.06" "Q" "R" "STU" "pid")("10.05" "L" "M" "N-O-PEE" "id10ts")("39.22" "H" "I" "MOM" "LovE")("122.22" "X" "Y" "ZED" "fail"))

Using (vl-sort rlst '(lambda (x y) (< (car x) (car y))))) results in:
(("1.02" "A" "B" "DEF" "id10t")("10.05" "L" "M" "N-O-PEE" "id10ts")("122.22" "X" "Y" "ZED" "fail")("2.06" "Q" "R" "STU" "pid")("39.22" "H" "I" "MOM" "LovE"))

I understand that (< (car x) (car y)) is sorting the first item of the list.  In this case the number.  What I can't figure out is how to modify that to sort the whole numbers.  i.e. 1, 2, 10, 39, 122.  I can't put zeros in front of the whole numbers to make them all 3 or more digits.  Everything after the decimal point doesn't matter, unless the whole number is the same.  There will always be two hundredths after decimal point.

Any pointers of where to start?  Can I substitute (< (car x) (car y)) with several lines of code to add zeros to the numbers to equal 5 digits, then  do the sort and then return the numbers back to the original?

All help is greatly appreciated.

Rabbit

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help with a vl-sort
« Reply #1 on: October 31, 2012, 08:07:24 PM »
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort rlst '(lambda ( a b ) (< (atof (car a)) (atof (car b)))))

Alternatively:
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( n ) (nth n rlst)) (vl-sort-i (mapcar '(lambda ( x ) (atof (car x))) rlst) '<))

PS: Formatting code in your posts.

Rabbit

  • Guest
Re: Help with a vl-sort
« Reply #2 on: November 01, 2012, 08:57:57 AM »
Once again, you saved my day Lee!  The first one works perfectly.  Looks like I was on the right track with substituting (< (car x) (car y)) with (< (atof (car a)) (atof (car b)).

And sorry about the formatting.  I usually do,  I just got a little lazy.

THANKS AGAIN!
Rabbit

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help with a vl-sort
« Reply #3 on: November 01, 2012, 09:01:54 AM »
You're welcome Rabbit  :-)