# Unidimensional Array Ascending Sort

Hi Guys…

I’ve got a problem…

There is an array with 300 occurrences and I need to sort the values there
in ascending position ignoring the duplicate values.
The array can contain 5 occurrences or 280 or 290. I will never know before…

Basically…

it’s a (n4/1:300)

let’s suppose that I have

4 (1)
5 (2)
9 (3)
120 (4)
4 (5)
6 (6)
130 (7)

I need it like this…

4(1)
5(2)
6(3)
9(4)
120(5)
130(6)

Long time ago, I use to have a cake’s receipt for it…but I tried to find everywhere
and I couldn’t…

Can someone send it to me please?

Tks

you can use the Natural SORT statement to sort arrays - include the index and value in the USING clause.

Not elegant, but here is some working code as per Doug’s previous post:

DEFINE DATA LOCAL
1 #A (N4/1:10)
INIT <23>
1 #B (N4/1:10)
1 #LOOP (I2)
1 #HOLD (N4)
1 #POS (I2) INIT <0>
END-DEFINE
*
FOR #LOOP = 1 TO 10
MOVE #A (#LOOP) TO #HOLD
END-ALL
SORT BY #HOLD USING #LOOP
MOVE #HOLD TO #B (#POS)
IF #POS GT 1 AND #HOLD = #B(#POS - 1)
SUBTRACT 1 FROM #POS
ESCAPE TOP
END-IF
DISPLAY #POS #HOLD #LOOP
END-SORT
*
END

steve

Trying to put in some elegance …

DEFINE DATA LOCAL
1 #T(N4/1:10) INIT <4>
1 #I(I2)
1 #N(N4)
END-DEFINE
FOR #I 1 10
IF #T(#I) NE 0
#N := #T(#I)
END-IF
END-ALL
SORT BY #N USING KEY
AT START OF DATA
RESET #I #T()
END-START
AT BREAK OF #N
#I := #I + 1
#T(#I) := OLD(#N)
END-BREAK
END-SORT
DISPLAY #T(
)
END

I have extended the previous only by a counter:

## DEFINE DATA LOCAL 1 #T(N4/1:10) /* INIT ALL <4> 1 #T-NUMBER (N4/1:10) 1 #I(I2) 1 #N(N4) 1 #NUMBER (I2) 1 #I-END (I2) END-DEFINE#T (1) := 4#T (2) := 4#T (3) := 184#T (4) := 4#T (5) := 4958#T (6) := 184#T (7) := 3058#T (8) := 3058#T (9) := 4#T (10) := 184 FOR #I = 1 TO 10 STEP 1 IF #T(#I) NE 0#N := #T(#I) END-IF END-ALL SORT BY #N USING KEY AT START OF DATA RESET #I#T(*) END-START AT BREAK OF #N#I := #I + 1#T(#I) := OLD(#N)#T-NUMBER (#I) := #NUMBER RESET #NUMBER END-BREAK ADD 1 TO #NUMBER END-SORT FOR #I = 1 TO 10 STEP 1 IF #T (#I) = 0 ESCAPE BOTTOM END-IF DISPLAY ‘Index’ #I ‘Value’ #T (#I) ‘Counter’ #T-NUMBER (#I) END-FOR END

The display is:

Index Value Counter

`````` 1       4      4
2    184      3
3  3058      2
4  4958      1``````

Use my QSORT & QDELDUPS from

http://scctoolkit.atspace.com