Author Topic: BFindV2 - sorting by length not alphabetically  (Read 230 times)

0 Members and 1 Guest are viewing this topic.

dami

  • Mosquito
  • Posts: 2
BFindV2 - sorting by length not alphabetically
« on: May 18, 2019, 07:30:32 AM »
Hello,
I'm using BFindV2 by Lee Mac (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

Dlanor

  • Newt
  • Posts: 144
Re: BFindV2 - sorting by length not alphabetically
« Reply #1 on: May 18, 2019, 08:10:33 AM »
Hello,
I'm using BFindV2 by Lee Mac (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))))))

dami

  • Mosquito
  • Posts: 2
Re: BFindV2 - sorting by length not alphabetically
« Reply #2 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 :-)

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1285
  • class keyThumper<T>:ILazy<T>
Re: BFindV2 - sorting by length not alphabetically
« Reply #3 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 ...
called Kerry in my other life

Sometimes the question is more important than the answer.

roy_043

  • Water Moccasin
  • Posts: 1686
  • BricsCAD 18
Re: BFindV2 - sorting by length not alphabetically
« Reply #4 on: May 19, 2019, 10:17:23 AM »
Probably '<' should be '>' in Dlanor's suggestion.

Dlanor

  • Newt
  • Posts: 144
Re: BFindV2 - sorting by length not alphabetically
« Reply #5 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: