View previous topic :: View next topic
|
Author |
Message |
CJT
New User
Joined: 02 Aug 2007 Posts: 83 Location: Here
|
|
|
|
Hi I have a requirement,
In My program i have an internal table that contains the tags:
This Tag ID is declared as PIC X(30) and it contains the following values
column1, column3 , column2, etc till column25. (name is as it appears)
They are all jumbled up inside the table
I would like to have it sorted as
column1 , column2, etc ... in an ascending order.
I did a bubble sort, but it appears like this
column1, column10, column11, column12, column2 etc
can anyone suggest how the sort can be accomplished. i would like the solution ASAP
thanks |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
You are getting what You asked for, exactly that ...
an alphanumeric sort
the reasonable expectation is to have the x picture fields filled with blanks x'40'
in the EBCDIC collating sequence the blanks(x'40') come before the alphanumeric chars
what You are asking for is not an alphanumeric sort, but some kind of tokenized sort
which means..
split the whole field in alphanumeric and numeric subfields properly aligned
and then carrying on the sort
in your sample means build an auxiliary array with
Code: |
row 1 - "column" "001"
row 2 - "column" "002"
....
row 10 - "column" "010"
...
row nnn - "column" "nnn" |
|
|
Back to top |
|
|
CJT
New User
Joined: 02 Aug 2007 Posts: 83 Location: Here
|
|
|
|
Heloo E.S,
I redefined the tag field as
Part 1 - Alphanumeric
Part2 - Numeric
and did the sort based on part 2. but it still doesnt work |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
redefine is not enough...
did You read carefully my answer with the sample
align align align |
|
Back to top |
|
|
CJT
New User
Joined: 02 Aug 2007 Posts: 83 Location: Here
|
|
|
|
I am sorry e.s,
i did not catch by what you meant by aligning |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
nothing to be sorry |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
the align/justify term means just ...
to put the spaces/blanks were they belong ( by convention/habit)
the alphabetic/alphanumeric fields ( letters and numbers used as characters )
are left aligned/justified ( filled to the right with spaces/blanks )
the numeric fields ( numbers used as numbers - let's not consider the signedness )
are right aligned/justified , filled to the left with spaces/zeroes
this is quite a rough approach...
for the sample You posted anyway the right sorted sequence is
- for the definition of alphanumeric sort and
- for the definition of a single "x pic" field
the one You got to start with
the issue is with the way our brain works ( very fast to associate/disassociate things )
it' s more natural to view as a sorted sequence "column1 ","column2 ","column11"
rather then the sortwise exact sequence "column1 ","column11",column2 "
( note the space at the end of the shorter string )
to be picky ( point out every little thing )
if You want to parse the data the way You asked
the field definition is not adequate
it should be really two fields, a char and a proper numeric |
|
Back to top |
|
|
CJT
New User
Joined: 02 Aug 2007 Posts: 83 Location: Here
|
|
|
|
Hi E.S,
thanks for showing the error.
Since according to the project requirement I was not suppsoed to change the declaration, I had to think in another way - like how you had suggested ... about aligning the variables correctly without spaces and so on.
I used a temp numeric variable.
Tag ID is defined as X(30)
Part 2 of TAG ID is numeric 1,2,3 etc
so took Part 2 of Tag ID stripped it of its spaces and moved it to another numeric variable
Now did a sort based on this numeric variable and it worked fine.
Here's the pseudocode
Code: |
ARRAY OF TAGS Indexed by IDX
TAG-ID PIC X(30)
TAG-ID-SPLIT redefines TAG-ID
TAG-ID-TEXT PIC X(20)
TAG-ID-NUM PIC X(10)
TEMP-NUM-1 PIC 9(10)
TEMP-NUM-2 PIC 9(10)
----------------------------
Procedure Division
Unstring TAG-ID-NUM(IDX) Delimited by spaces into
TEMP-NUM-1
Unstring TAG-ID-NUM(IDX + 1 ) Delimited by spaces into
TEMP-NUM-2
Bubble Sort based on TEMP-NUM-1 and TEMP-NUM-2
|
|
|
Back to top |
|
|
|