string to variable conversion

Posted by ErockMahan on Thu 20 Feb 2014 04:05 PM — 2 posts, 15,539 views.

#0
I've got a bit of a mess here that I'm trying to clean up. I have a variable with numbers put in to in a random order, separated by a space:


numstring = "23 5 187 13"


I want to sort it, so I am using this:


bits = Split(numstring, " ")

for a = UBound(bits) - 1 To 0 Step -1
for j=0 to a
if bits(j)>bits(j+1) then
temp=bits(j+1)
bits(j+1)=bits(j)
bits(j)=temp
end if
next
next


The PROBLEM I have is that this isn't sorting it numerically, it looks like it is sorting it alphabetically. That means that the end result is this:


5 23 187 13


Is there any way I can do this?
Amended on Thu 20 Feb 2014 07:56 PM by ErockMahan
Australia Forum Administrator #1
For the comparison I suggest you convert to numbers (I don't remember how to do this) instead of comparing strings.