# Put mass selected integers in ascending order starting from a given integer

## Recommended Posts

Hello,

Let's say we have a group of integers as text in dwg.

How would we put them in in ascending order by one(+1) starting from a given integer?

Let me give an example to make it clear. Let's say we have the texts 1 2 5 5 5 8 9 11 11 15 and the given start integer is 6. We select them all together and the range in selection set is random. Let's say 5 2 8 15 1 5 5 11 9 11.

Now, we want to put them in ascending order by one starting from number 6 replacing the "old" range with the new. In the end we must have this:

6 7 8 8 8 9 10 11 11 12.

That is 6(in place of 1), 7(in place of 2), 8(5), 8(5), 8(5), 9(, 10(9), 11(11), 11(11), 12(15). I hope it is clear.

How would we do that in an efficient way?

Kostas

9(, = 9 ( 8 )

##### Share on other sites

Consider the following code:

```(defun renumberlist ( num lst )
(setq lst (vl-sort lst '(lambda ( a b ) (< (atoi a) (atoi b))))
num (1- num)
)
(mapcar '(lambda ( a b ) (if (= a b) (itoa num) (itoa (setq num (1+ num))))) (cons nil lst) lst)
)```

Example:

```_\$ (renumberlist 6 '("5" "2" "8" "15" "1" "5" "5" "11" "9" "11"))
("6" "7" "8" "8" "8" "9" "10" "11" "11" "12")```

##### Share on other sites

Consider the following code:

Its impressive how fast you understand peoples tasks/problems.

##### Share on other sites

Thank you Lee Mac! I will try to import your code to my routine. If I don't manage, I will come back to you. (It's very possible because your coding is in much higher level than mine). Thanks!

##### Share on other sites

Its impressive how fast you understand peoples tasks/problems.

Thanks

Thank you Lee Mac! I will try to import your code to my routine. If I don't manage, I will come back to you. (It's very possible because your coding is in much higher level than mine). Thanks!

You're welcome tsotzo. My code demonstrates one possible method of ordering & renumbering a set of text values, however, assuming you are looking to then modify the text content, you will need to retain the entity name of the text object during the sorting & renumbering process - I'm happy to demonstrate this if you get stuck.

##### Share on other sites

Just for fun, here's a recursive function accepting a sorted list to produce the same result:

```(defun renum ( num lst )
(if lst (cons (itoa num) (renum (if (= (car lst) (cadr lst)) num (1+ num)) (cdr lst))))
)```

Example:

```_\$ (renum 6 (vl-sort '("5" "2" "8" "15" "1" "5" "5" "11" "9" "11") '(lambda ( a b ) (< (atoi a) (atoi b)))))
("6" "7" "8" "8" "8" "9" "10" "11" "11" "12")```

That's right!

##### Share on other sites

OK, I couldn't resist

```([color=BLUE]defun[/color] c:renum ( [color=BLUE]/[/color] a b i l n s x y z )
([color=BLUE]initget[/color] 4)
([color=BLUE]setq[/color] n ([color=BLUE]1-[/color] ([color=BLUE]cond[/color] (([color=BLUE]getint[/color] [color=MAROON]"\nSpecify starting number <1>: "[/color])) (1))))
([color=BLUE]if[/color] ([color=BLUE]setq[/color] s ([color=BLUE]ssget[/color] [color=MAROON]"_:L"[/color] '((0 . [color=MAROON]"TEXT"[/color]) (1 . [color=MAROON]"~*[~0-9]*"[/color]))))
([color=BLUE]progn[/color]
([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] s))
([color=BLUE]setq[/color] x ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] s ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i))))
a ([color=BLUE]cons[/color] ([color=BLUE]assoc[/color] -1 x) a)
b ([color=BLUE]cons[/color] ([color=BLUE]atoi[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 1 x))) b)
)
)
([color=BLUE]setq[/color] i ([color=BLUE]vl-sort-i[/color] b '[color=BLUE]<[/color])
l ([color=BLUE]mapcar[/color] '([color=BLUE]lambda[/color] ( n ) ([color=BLUE]nth[/color] n b)) i)
)
([color=BLUE]mapcar[/color] '([color=BLUE]lambda[/color] ( x y z ) ([color=BLUE]entmod[/color] ([color=BLUE]list[/color] ([color=BLUE]nth[/color] z a) ([color=BLUE]cons[/color] 1 ([color=BLUE]itoa[/color] ([color=BLUE]if[/color] ([color=BLUE]=[/color] x y) n ([color=BLUE]setq[/color] n ([color=BLUE]1+[/color] n)))))))) ([color=BLUE]cons[/color] [color=BLUE]nil[/color] l) l i)
)
)
([color=BLUE]princ[/color])
)
```

##### Share on other sites

& & . Lee Mac, when I first read your message I laughed with my heart and then... wow... I have no words...! You are unbelievable! You know, I had written more than double of code lines than you, and I wasn't even in the middle! What can I say... Thanks man!! You know, what's impressive is not only that you solve the problem, but the way that you do it (and the speed of course ). Thanks again!
##### Share on other sites

& & . Lee Mac, when I first read your message I laughed with my heart and then... wow... I have no words...! You are unbelievable! You know, I had written more than double of code lines than you, and I wasn't even in the middle! What can I say... Thanks man!! You know, what's impressive is not only that you solve the problem, but the way that you do it (and the speed of course ). Thanks again!

Thank you for your kind words & compliments, you're most welcome.

This was an enjoyable program to write, and so after pointing you in the right direction, I couldn't resist coding a solution

I welcome you to continue with your attempt - there is often more than one way to solve the problem, and nothing is more valuable in programming than practice!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.