How to sort a string

How to sort a string

Anonymous
Not applicable
310 Views
8 Replies
Message 1 of 9

How to sort a string

Anonymous
Not applicable
I am doing a tblsearch to collect layer names (which are sheet numbers)
that I wish to display. However, the layer table is apparently not in
alphanumeric order so I get a display like 1,2,3,4,6,9,10,5. This string
has been constructed with strcat and is saved as a variable. How can I
sort the contents of the variable so it would display as
1,2,3,4,5,6,9,10.

TIA,
Cliff
0 Likes
311 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Use the (acad_strlsort) function.

See following example:

(setq h nil)
(setq a (cdr (assoc 2 (tblnext "layer" "T"))))
(setq h (append h (list a)))
(while (setq a (cdr (assoc 2 (tblnext "layer"))))
(setq h (append h (list a)))
)
(setq h (acad_strlsort h))

R.K. McSwain
[email protected]

Cliff DuHaime wrote in message
news:[email protected]...
> I am doing a tblsearch to collect layer names (which are sheet numbers)
> that I wish to display. However, the layer table is apparently not in
> alphanumeric order so I get a display like 1,2,3,4,6,9,10,5. This string
> has been constructed with strcat and is saved as a variable. How can I
> sort the contents of the variable so it would display as
> 1,2,3,4,5,6,9,10.
>
> TIA,
> Cliff
>
>
0 Likes
Message 3 of 9

Anonymous
Not applicable
This may not necessarily produce the desired result. If the drawing contained
layers 0, 9 and 10, the code would return:

("0","10","9")

If you are certain that the layers will always be numbers, you should use
(atoi) to convert the names to a list of numbers, sort them and then use
(itoa) to convert back to strings.

Regards,

Andrew Wilford
0 Likes
Message 4 of 9

Anonymous
Not applicable
Cliff,

I addition to using (acad_strlsort) as R.K. suggested, a function to append
leading zeros to the number strings will produce the desired results. Here's
the Function:

;; ----- (lead-zeros [No of Digits][String]) ------------
;; Given a Numeric String and Total number of Digits, This Function
;; Returns a String With the Correct Amount of Leading Zeros.
(defun lead-zeros (digits str / cnt done z-str)
(setq cnt 1 z-str "" done nil)
(repeat digits
(if (and (= (strlen str) cnt)(= done nil))
(progn
(if (/= cnt digits)
(repeat (- digits cnt)(setq z-str (strcat "0" z-str)))
)
(setq str (strcat z-str str))
(setq done T)
)
)
(setq z-str "" cnt (1+ cnt))
)
str
);; End Function (lead-zeros)

Apply this within a loop to rebuild a new list of strings containing the
leading zeros, then use (acad_strlsort) on the rebuilt list.

Regards,

--
Phillip Kenewell
CAD Systems Technician
Air Gage Company
[email protected]

Cliff DuHaime wrote in message
news:[email protected]...
> I am doing a tblsearch to collect layer names (which are sheet numbers)
> that I wish to display. However, the layer table is apparently not in
> alphanumeric order so I get a display like 1,2,3,4,6,9,10,5. This string
> has been constructed with strcat and is saved as a variable. How can I
> sort the contents of the variable so it would display as
> 1,2,3,4,5,6,9,10.
0 Likes
Message 5 of 9

Anonymous
Not applicable
The following function will sort a list of strings, sorting string
representations of integers as you wish, alpha strings normally.

e.g.

(sort_strs '("19" "0" "9" "6" "11" "1"))

will return ("0" "1" "6" "9" "11" "19")

(defun sort_strs (strs / len x y i j)
(cond
( acad_strlsort
(setq len 0)
(foreach x strs (setq len (max (strlen x) len)))
(mapcar
'(lambda (y)
(while (and (< (setq j (ascii y)) 33) (< 0 j))
(setq y (substr y 2))
)
y
)
(acad_strlsort
(mapcar
'(lambda (x)
(if (and (< (setq i (ascii x)) 58) (< 47 i))
(repeat (- len (strlen x)) (setq x (strcat " " x)))
)
x
)
strs
)
)
)
)
( t (princ "\nDoh! function is unavailable.") strs )
)
)

Throwing it inside a wrapper to return a sorted list of layers in the
drawing:

(defun get_layers ( / d r)
(while (setq d (tblnext "layer" (null d)))
(setq r (cons (cdr (assoc 2 d)) r))
)
(sort_strs r)
)

I have another routine to sort integers; let me know if you'd like it.

Cheers.

______________________________

[email protected]
Not a Member of the AutoDESK
Discussion Forum Moderator Program
Imagination makes all things possible
______________________________

Cliff DuHaime wrote in message
news:[email protected]...
I am doing a tblsearch to collect layer names (which are sheet numbers)
that I wish to display. However, the layer table is apparently not in
alphanumeric order so I get a display like 1,2,3,4,6,9,10,5. This string
has been constructed with strcat and is saved as a variable. How can I
sort the contents of the variable so it would display as
1,2,3,4,5,6,9,10.

TIA,
Cliff
0 Likes
Message 6 of 9

Anonymous
Not applicable
Wow, what a response! This will take some time to sort out . Thanks
everyone, I'll let you know how I make out.

Cliff
0 Likes
Message 7 of 9

Anonymous
Not applicable
(strlgather (vmergesort (strtok STRNG ",") atoi) ",")
using functions from my site. 🙂

Point being, it's good to develop some library
functions for common tasks.

{{in English the code means -- break the string down
to pieces by comma, then sort by atoi function, then
glue'em back with commas again. }}

HTH

On Tue, 21 Sep 1999 13:46:40 -0400, Cliff DuHaime
wrote:

>I am doing a tblsearch to collect layer names (which are sheet numbers)
>that I wish to display. However, the layer table is apparently not in
>alphanumeric order so I get a display like 1,2,3,4,6,9,10,5. This string
>has been constructed with strcat and is saved as a variable. How can I
>sort the contents of the variable so it would display as
>1,2,3,4,5,6,9,10.
>
>TIA,
>Cliff
>

--
Vlad http://www.netvision.net.il/php/vnestr/
0 Likes
Message 8 of 9

Anonymous
Not applicable
(Doesn't sort string representations of negative integers correctly).

________________________________

[email protected]
>Not< a Member of the AutoDESK
Discussion Forum Moderator Program
Imagination makes all things possible.
________________________________
0 Likes
Message 9 of 9

Anonymous
Not applicable
I reviewed all of your responses and comments. And while I originally
thought I would somehow use the acad_strlsort function, that function did
not seem to completely sort in the manner I desired. The end result would be
to have my code display on the command line as follows: 1,2,3,4,9,15
instead of the way that tblnext brings it up (by creation number?):
1,2,15,9,4 which could represent how the designer was creating sheet layers,
as bogus as it seems. At any rate, here is the code I came up with - note
thats its only a portion of the full program.

(tblnext "layer" T)
(while (setq p2 (tblnext "layer")) ;find highest sheet
number
(setq p2 (cdr (assoc 2 p2 )))
(setq p1 (substr p2 1 1 ))
(if (= p1 "S")
(progn
(if (= e nil)
(setq e 0)
)
(setq d (atoi (substr p2 2)))
(setq e (max d e))
)
)
)
(setq f 0) ;construct the sheet
list
(while (<= f e)
(setq f (+ 1 f))
(setq g (strcat "S" (itoa f)))
(if (tblsearch "layer" g)
(cond
((= p3 nil)(setq p3 (substr g 2)))
((> e 1)(setq p3 (strcat p3 "," (substr g 2))))
)
)
)
(setq w 1)
(while w ;user input
(princ "\nAvailable sheets: ")
(princ p3)
(setq a (getint "\nEnter sheet number: "))
(if (not (tblsearch "layer" (strcat "S" (itoa a))))
(alert "\nInvalid sheet number")

I have the feeling that I could probably do the same thing with fewer lines,
but this is what I came up with that seems to work, but I have not
completely tested it yet.

Thanks everyone for the ideas,
Cliff
0 Likes