# SORT in Natural

**URL:** <https://techcommunity.softwareag.com/t/sort-in-natural/61673>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Mainframes\
**Created:** [November 6, 2006, 9:28am UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673 "2006-11-06T09:28:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rich](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@Rich](https://techcommunity.softwareag.com/u/Rich)\
**Post date:** [November 6, 2006, 9:28am UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/1 "2006-11-06T09:28:58Z")

</div>

Hi,

While reading a adabas file, i get my records as below:

STATUS DATE TIME IND

* * *

A 20061102 030347 A  
A 20061102 030347 D  
A 20061102 030347 R  
Z 20061102 023837 A  
Z 20061102 024929 A  
Z 20061102 023837 D  
Z 20061102 024929 D  
Z 20061102 023837 R  
Z 20061102 024929 R

Could anyone please suggest the best way to fetch the most recent records out of these for IND = A,D,R  
i would like to sort this on Date and time and get 3 latest records one each for A,D,R

like here  
A 20061102 030347 A  
A 20061102 030347 D  
A 20061102 030347 R

Incase ‘A’ status record werenot there and my Read gives below ,

Z 20061102 023837 A  
Z 20061102 024929 A  
Z 20061102 023837 D  
Z 20061102 024929 D  
Z 20061102 023837 R  
Z 20061102 024929 R

I would have liked the sort results to be

Z 20061102 024929 A  
Z 20061102 024929 D  
Z 20061102 024929 R

Thanks in advance.

Regards,  
Rik

---

<div class="post-metadata">

**Author:** ![Douglas\_Kelly](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/douglas_kelly/32/102_2.png) [@Douglas\_Kelly](https://techcommunity.softwareag.com/u/Douglas_Kelly)\
**Post date:** [November 6, 2006, 10:12am UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/2 "2006-11-06T10:12:44Z")

</div>

based on what you have described here, I would probably set up a superdescriptior on IND, Date, Time and Status. Use 3 READ DESCENDING statements (one for each IND) (either 3 READ statements in the program or one READ statement inside a FOR loop to start the loop with each IND value).

I assumed that the “in case A status record is not there” would be a daily thing - records for the “A” or “Z” status may or may not occur on any given day.

---

<div class="post-metadata">

**Author:** ![Rich](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@Rich](https://techcommunity.softwareag.com/u/Rich)\
**Post date:** [November 6, 2006, 10:45am UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/3 "2006-11-06T10:45:56Z")

</div>

Thanks for reply. But i cannot set up a Descriptor field. I have to use the ones available with me which doesnot include the one as mentioned by you.

And I Need to read all these records first to do some processing. Then out of all these , i need the latest for A,D,R ind for some other processing.

So, i would prefer a way to sort these records from the complete list of records i get on reading the file.

---

<div class="post-metadata">

**Author:** ![Wilfried\_B](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/wilfried_b/32/83_2.png) [@Wilfried\_B](https://techcommunity.softwareag.com/u/Wilfried_B)\
**Post date:** [November 6, 2006, 2:23pm UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/4 "2006-11-06T14:23:07Z")

</div>

In the read loop, store the youngest record you found for each IND value. A status ‘Z’ can only overwrite a status ‘Z’ but not a status ‘A’ value. Sorting is not necessary as you already read all records.

---

<div class="post-metadata">

**Author:** ![Rich](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@Rich](https://techcommunity.softwareag.com/u/Rich)\
**Post date:** [November 6, 2006, 2:54pm UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/5 "2006-11-06T14:54:47Z")

</div>

Hi Wilfried,

Could you please tell the same with ex.I am afraid i didnot understand your reply well.Thankyou.How do i store the youngest record .  
Do you mean something like this.  
If ind=‘Z’  
f on-line-date GT on-line-date-temp or  
(on-line-date EQ on-line-date-temp and  
on-line-time gt on-line-time-temp)

where temp would contain the last rec read date and time for IND = ‘z’?

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [November 6, 2006, 6:04pm UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/6 "2006-11-06T18:04:48Z")

</div>

Hi Rik;

What is not clear from your question is the role of “Status” in determining the most recent record. Suppose you had:

Z 20061102 023837 A  
A 20061101 024929 A

Based on just date and time, the first record is the most recent. However, the second record is an “A” while the first record is a “Z”. Is the Status field relevant for determining “most recent”?

Regardless of the answer above, you will want to have three IF tests for each record. Logically they would be:

```
                         READ record
                         process record...
                         IF current record IND = 'A'
                             IF current record date-time older than newest-a
                                 escape top
                                 else
                                 move current record isn and date-time to newest-a
                                 escape top
                                 end-if

```

and you would have the same sort of code for D and R.

steve

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:40pm UTC](https://techcommunity.softwareag.com/t/sort-in-natural/61673/7 "2021-04-02T14:40:56Z")

</div>

Closed on April 2, 2021, 2:40pm UTC


