As you might notice that first 2 records which are headers, are not a valid packed decimal. DFSORT do not complain about it and sorts all valid packed decimals in correct order. But among Header records 'MS67' comes before 'MA82', the sort order is unexplained it always puts them in same order even if they appear in reverse order in sortin.
It means to me that dfsort in not ignoring these invalid packed decimals and always puts them in same order(MS67 THEN MA82).
Now I am not able to explain why sort puts them in this order?.
Joined: 18 Jul 2007 Posts: 2150 Location: At my coffee table
Reasonable problem, if there is a way to identify the headers versus the actual data.....So far the only difference between the headers and data is that field in the header would fail a COBOL 'IF NUMERIC' test.......
Do you want the data between the headers sorted but the headers left in their original order? An example of what you want your output (based upon the input you provided) would be helpful.
Yes I have means to identify the header versus detail record but I am not looking for putting them in particular order.
when I sort them using above sort statement:- they look like
i.e MS67 stays at TOP but MA82 Goes in Bottom.
I have ways to handle it to make sure headers come together.
But the question here is How can we explain why MS67 comes at TOP and MA82 goes in bottom.
How sort fields=(1,4,pd,a) responds when it finds a non numeric fields to be sorted and in what order they(non numeric fields) are sent in sortout.
DFSORT do not complain about it and sorts all valid packed decimals in correct order. But among Header records 'MS67' comes before 'MA82', the sort order is unexplained it always puts them in same order even if they appear in reverse order in sortin.
DFSORT does not flag invalid PD sort values - it just interprets them as PD values and sorts them. It treats the last nibble as the sign as follows:
Positive signs: F, E, C, A, 8, 6, 4, 2, 0
Negative signs: D, B, 9, 7, 5, 3, 1
So MS67 is treated as negative (last nibble is 7 = negative) and MA82 is treated as positive (last nibble is 2 = positive).
The actual sorted records you would get for your example are:
The first header is treated as negative (7 for sign) so it's first. The other values are treated as positive (C and 2 for sign). Since D is greater than 0, the second header is last.
You should take steps to keep the headers in place. You can use the header part of the technique discussed in the "Sort records between a header and trailer" Smart DFSORT Trick at:
That was exactly I was looking for. Thanks a Ton for the help.
I was challenged by one of the reviewer to prove that MS67 would always come first. All our team is delighted by your perfect explanation.