# DFSORT – Simple sort with multiple columns using SORT FIELDS

This tutorial will teach you the very basics of IBM’s DFSORT. DFSORT’s main purpose is to sort, however it does a lot more than the sort – basically SORT can do almost any types of data processings.

Lets say you have a sequential dataset (PS) with below data. It has 3 fields –

• employee name
• employee salary
• department that employee belongs to

INPUT DATA:

```----+----1----+----2----+----3----+----4----+----5
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR
```

Now we want to sort it based on department, employee name then the salary.

To achieve this we need to tell SORT from which columns each of the fields are starting and their sort order.

To sort the department column which starts from column 13 by ascending order we need to feed below to SORT:

```(13,8,CH,A)
```

13 = Starting column
8 = Length of the string
CH = Character type data
A = Ascending Order

Similarly for employee name and salary we can put the followings:

Putting it all together, SORT input would be –

```(13,8,CH,A,1,5,CH,A,7,5,CH,A)
```

Complete JCL:

```//YOUR.JOB.CARD.HERE
//*
//STEP1 EXEC PGM=SORT
//** You can give your input PS file here instead of in stream data
//SORTIN DD *
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR
/*
//** You can give your Output PS file here instead of in stream data
//SORTOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SYSIN DD *
SORT FIELDS=(13,8,CH,A,1,5,CH,A,7,5,CH,A)
/*
```

OUTPUT:

```----+----1----+----2----+----3----+----4----+----5
SAM   11234 ACCOUNTS
STEVE 62541 ACCOUNTS
TIM   90013 HR
JOHN  78916 IT
```

Drop a comment if you like this article or face any problem using the above code.

Translate »