sean
New Member
Posts: 2
|
Post by sean on Jan 22, 2019 17:06:54 GMT -5
Hi,
I have a set of 5 numbers ranging from 1.2 to 100. I have these in an array but i want to sort them so they are in another array from the lowest number to the highest.
For example, I have
array1$(1)="1.2" array1$(2)="100" array1$(3)="10" array1$(4)="14.5" array1$(5)="3"
And i want to sort these into another array so they are arranged like this
array1$(1)="1.2" array1$(2)="3" array1$(3)="10" array1$(4)="14.5" array1$(5)="100"
I have tried the code max(a,b) using the val code which tells me the highest number from 2 numbers but i just don't know how to do it with 5numbers. I also tried the sort code to sort the array but it doesn't sort them from high to low. I have tried everything but can seem to work out how to do it. Could someone give me some help? Whats the best way of doing this?
Thanks Sean
|
|
fwm
Full Member
Posts: 105
|
Post by fwm on Jan 22, 2019 17:49:21 GMT -5
You are correct in using the sort function, but you are sorting string values rather than numeric values, so it will sort them into all strings that begin with 1, then all that begin with 2, etc. Try this instead: array1(1)=1.2
array1(2)=100
array1(3)=10
array1(4)=14.5
array1(5)=3
sort array1(), 1, 5
for index = 1 to 5
print array1(index)
next index
So in other words, using numeric variables instead of strings. If you need them to stay as strings then you'll need to do some fancy stuff to either add zeros (e.g. 001.2, 100, 010, 014.5, 003) then sort them, then remove the zeros to display them, or convert the strings to a temporary numeric array, sort it, then convert it back to a string array. I can't think of another easier way to do it, but perhaps someone else can Keith
|
|
sean
New Member
Posts: 2
|
Post by sean on Jan 22, 2019 18:06:51 GMT -5
Thanks Keith! That's solved the problem. I think its simple enough for me! I had them in strings but I can do what I need to do as numeric variables. Thanks again Sean
|
|