Sorting by multiple fields

You can further sort the records in the bookstore data set by specifying multiple control fields. When you specify two or more control fields, you specify them in the order of greater to lesser priority. Note that control fields might overlap or be contained within other control fields.

Table 1 shows how the records would be sorted if you specified the following control fields in the order they are listed:
  1. Course department
  2. Course number
  3. Instructor's last name
  4. Instructor's initials
  5. Book title.

So, if two records have the same department, they are sorted by course number. If they also have the same course number, they are sorted by instructor's last name. If they also have the same last name, they are sorted by initials. Finally, if they also have the same initials, they are sorted by title.

Specify the location, length, data format, and order for each of the control fields, as follows:
Specifying the location, length, data format, and order for each of the control fields

The records are sorted as shown in Table 1.

Table 1. Sample Bookstore Data Set Sorted by Multiple Fields
Book Title
Course
Department
Course
Number
Instructor's
Last Name
Instructor's
Initials
1          75
110    114
115   119
145    159
160   161
LIVING WELL ON A SMALL BUDGET
PICK'S POCKET DICTIONARY
INTRODUCTION TO BIOLOGY
STRATEGIC MARKETING
SUPPLYING THE DEMAND
NUMBERING SYSTEMS
COMPUTER LANGUAGES
COMPUTERS: AN INTRODUCTION
SYSTEM PROGRAMMING
VIDEO GAME DESIGN
SHORT STORIES AND TALL TALES
EDITING SOFTWARE MANUALS
THE COMPLETE PROOFREADER
INKLINGS: AN ANTHOLOGY OF YOUNG POETS
MODERN ANTHOLOGY OF WOMEN POETS
THE INDUSTRIAL REVOLUTION
CRISES OF THE MIDDLE AGES
EIGHTEENTH CENTURY EUROPE
INTRODUCTION TO PSYCHOLOGY
ADVANCED TOPICS IN PSYCHOANALYSIS
 
 
BIOL
BUSIN
BUSIN
COMP
COMP
COMP
COMP
COMP
ENGL
ENGL
ENGL
ENGL
ENGL
HIST
HIST
HIST
PSYCH
PSYCH
 
 
80521
70124
70251
00032
00032
00032
00103
00205
10054
10347
10347
10856
10856
50420
50521
50632
30016
30975
 
 
GREENBERG
LORCH
MAXWELL
CHATTERJEE
CHATTERJEE
CHATTERJEE
SMITH
NEUMANN
BUCK
MADRID
MADRID
FRIEDMAN
FRIEDMAN
GOODGOLD
WILLERTON
BISCARDI
ZABOSKI
NAKATSU
 
 
HC
HH
RF
AN
CL
CL
DC
LB
GR
MM
MM
KR
KR
ST
DW
HR
RL
FL

You can often shorten the length of control statements. You can specify fields together whenever they are next to each other in the record and have the same data format and order. You can shorten this last statement by specifying the department and course number together as one field, and the instructor's last name and initials together as one field.

Shortening this last statement by specifying the department and course number together as one field, and the instructor's last name and initials together as one field
Also, if all of the control fields have the same data format, you can specify the data format just once, using the FORMAT=f parameter. For example:
 SORT FORMAT=CH,FIELDS=(110,10,A,145,17,A,1,75,A)
If some of the control fields have the same data format, but others don't, you can specify the FORMAT=f parameter along with the individual data formats. For example:
 SORT FORMAT=CH,FIELDS=(110,10,A,170,4,BI,D,145,17,A,1,75,A)
is equivalent to:
 SORT FIELDS=(110,10,CH,A,170,4,BI,D,145,17,CH,A,1,75,CH,A)
example : Sorting UTF16 data and Character data
SORT FIELDS=(5,4,FI,A,345,400,UTF16,D,13,2,CH,A)