Monday, June 25, 2012

[Action Script] Insert Number into a sequence June,2012

I am looking for ideas to write a function that can insert an arbitary number into an arbitary sequence of numbers in an array, so that the result is in an ascending order.

ActionScript Code: var _objects:Vector.<int>; = Vector.<int>([ 1, 2, 4, 6, 7 ]);insertNum(5); // i want this: 1, 2, 4, 5, 6, 7
- I first thought of looping through the array and inserting the number after the first number that it is larger, but that would result in:
1, 2, 5, 4, 6, 7, where the 5 precedes 4. Similarly if I loop backwards, and I have the number 3 for example, then the result could be 1, 2, 5, 3, 6, 7

NOTE: Using sort, or sortOn() is NOT a possibility (as it is a simplified example, in the real case these numbers would be properties on a vector array of Objects)

PS: the array can only receive new elements using this method, so it will always be sorted, although it may have gaps
Insert Number into a sequence

Related Post



0 comments: