TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dami on May 18, 2019, 07:30:32 AM

Title: BFindV2 - sorting by length not alphabetically
Post by: dami on May 18, 2019, 07:30:32 AM
Hello,
I'm using BFindV2 by Lee Mac (http://www.lee-mac.com/bfind.html (http://www.lee-mac.com/bfind.html)) and came across a problem when source text cannot be found because it contained the word which have been already replaced. Easy solution for this can be just sorting all elements of list by lenght - longer element can not be included in shorter one.
I noticed that elements of the list are being sorted alphabetically. Was trying to get into code, but have never using LISP before.

Probably this method is doing the job:
Code: [Select]
  (defun _SortByFirst ( lst )
    (vl-sort lst (function (lambda ( a b ) (< (car a) (car b)))))
  )

I was trying this:
Code: [Select]
(vl-sort lst (function (lambda ( a b ) (< (length a) (length b)))))
...but It does not work. I probably misunderstand the code.

Please, can you guys help me?
best,
D
Title: Re: BFindV2 - sorting by length not alphabetically
Post by: Dlanor on May 18, 2019, 08:10:33 AM
Hello,
I'm using BFindV2 by Lee Mac (http://www.lee-mac.com/bfind.html (http://www.lee-mac.com/bfind.html)) and came across a problem when source text cannot be found because it contained the word which have been already replaced. Easy solution for this can be just sorting all elements of list by lenght - longer element can not be included in shorter one.
I noticed that elements of the list are being sorted alphabetically. Was trying to get into code, but have never using LISP before.

Probably this method is doing the job:
Code: [Select]
  (defun _SortByFirst ( lst )
    (vl-sort lst (function (lambda ( a b ) (< (car a) (car b)))))
  )

I was trying this:
Code: [Select]
(vl-sort lst (function (lambda ( a b ) (< (length a) (length b)))))
...but It does not work. I probably misunderstand the code.

Please, can you guys help me?
best,
D

Perhaps

Code - Auto/Visual Lisp: [Select]
  1.  (vl-sort lst (function (lambda ( a b ) (< (strlen (car a)) (strlen (car b))))))
Title: Re: BFindV2 - sorting by length not alphabetically
Post by: dami on May 18, 2019, 05:32:49 PM
Nope, it doesn't sort properly.
Well, I just sorted my .txt file and left fuction in such silly definition:
Code: [Select]
(defun _SortByFirst ( lst )
    lst
  )

At least I achieved what I wanted :-)
Title: Re: BFindV2 - sorting by length not alphabetically
Post by: kdub_nz on May 18, 2019, 06:02:52 PM
Nope, it doesn't sort properly.
Well, I just sorted my .txt file and left fuction in such silly definition:
Code: [Select]
(defun _SortByFirst ( lst )
    lst
  )

At least I achieved what I wanted :-)


It's very difficult not to laugh ...
Title: Re: BFindV2 - sorting by length not alphabetically
Post by: roy_043 on May 19, 2019, 10:17:23 AM
Probably '<' should be '>' in Dlanor's suggestion.
Title: Re: BFindV2 - sorting by length not alphabetically
Post by: Dlanor on May 19, 2019, 07:23:01 PM
Probably '<' should be '>' in Dlanor's suggestion.

Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst (function (lambda ( a b ) (< (length a) (length b)))))

I was just following the OP's attempt  :whistling: