The SORT() function can only do left-justified sorts and there is nothing
intrinsic in jBASE that will do this for you.

The actual solution depends on how large the list of numbers, that needs to
be sorted, is.

I'm sure other members will chime in with better/faster solutions but one
solution is to use LOCATE, e.g.

0001     TEST = -1 : @FM : 4 :@FM: -399 : @FM : 19
0002     N = DCOUNT(TEST,@FM)
0003     TEST1 = ''
0004     FOR X = 1 TO N
0005         NUMBER = TEST<X>
0006         LOCATE NUMBER IN TEST1 BY "AN" SETTING POS ELSE NULL
0007         INS NUMBER BEFORE TEST1<POS>
0008     NEXT X
0009     CRT TEST
0010     CRT TEST1

Another solution would be to use a binary search and insertion (ref.
Knuth's TAOCP Vol 3, Sorting and Searching), e.g.

0001     unsortedNumbers = -1 : @FM : 4 :@FM: -399 : @FM : 19
0002     sortedNumbers = ''
0003     n = DCOUNT(unsortedNumbers, @FM)
0004     FOR x = 1 TO n
0005         number = unsortedNumbers<x>
0006         found = 0
0007         lowerBound = 1
0008         upperBound = DCOUNT(sortedNumbers, @FM)
0009         LOOP UNTIL lowerBound > upperBound OR found DO
0010             midPoint = INT((lowerBound + upperBound) / 2)
0011             midValue = sortedNumbers<1,midPoint>
0012             IF number = midValue THEN found = midPoint ELSE
0013                 IF number < midValue THEN upperBound = midPoint - 1
ELSE lowerBound = midPoint + 1
0014             END
0015         REPEAT
0016         IF NOT(found) THEN found = lowerBound
0017         INS number BEFORE sortedNumbers<found>
0018     NEXT x
0019     CRT unsortedNumbers
0020     CRT sortedNumbers

I hear-tell that Quicksort would be another option.

I suspect the binary search method will be faster but ymmv depending on how
many numbers are in the original list.

But what do I know ;-)

Dan


On Mon, Jan 30, 2012 at 5:40 AM, master <[email protected]> wrote:

> Hi,
>
> Is there a function to sort numerics (float and ints) in jbase?
> Sample this:
>
> TEST = -1 : @FM : 4 :@FM: -399 : @FM : 19
> TEST2 = SORT(TEST)
>
> This gives
>  TEST2                     : -1^-399^19^4
>
> The function treats the values as string and therefore the result is,
> as per the expected outcome, wrong.
>
> --
> IMPORTANT: T24/Globus posts are no longer accepted on this forum.
>
> To post, send email to [email protected]
> To unsubscribe, send email to [email protected]
> For more options, visit this group at
> http://groups.google.com/group/jBASE?hl=en
>

-- 
IMPORTANT: T24/Globus posts are no longer accepted on this forum.

To post, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en

Reply via email to