Newer Version Available

This content describes an older version of this product. View Latest

order

Sorts in ascending or descending order on one or more fields.

Syntax

1result = order rows by field [ asc | desc ];
2result = order rows by (field [ asc | desc ], field [ asc | desc ]);

asc or desc specifies whether the results are ordered in ascending (asc) or descending (desc) order. The default order is ascending.

Usage

The order statement isn’t applied to the whole set. The order statement operates on rows individually.

You can use the order statement with ungrouped data. You can also use the order statement to specify order within a group or to sort grouped data by an aggregated value.

Example

q = order q by 'count' desc;

Example

To order a stream by multiple fields, use this syntax:
1a = load "0Fbxx000000002qCAA/0Fcxx000000002WCAQ";
2b = group a by (year, month);
3c = foreach b generate year as year, month as month;
4d = order c by (year desc, month desc);

Example

You can order a cogrouped stream before a foreach statement:
1a = load "0Fbxx000000002qCAA/0Fcxx000000002WCAQ"; 
2b = load "0Fayy000000002qCAA/0Fbyy000000002WCAQ"; 
3c = cogroup a by year, b by year; 
4c = order c by a.airlineName; 
5c = foreach c generate year as year;

Example

You can’t reference a preprojection ID in a postprojection order operation. (Projection is another term for a foreach operation.) This code throws an error:
1q = load "0Fbxx000000002qCAA/0Fcxx000000002WCAQ";
2q = group q by 'FirstName';
3q = foreach q generate sum('mea_mm10M') as 'sum_mm10M';
4q = order q by 'FirstName' desc;
This code is valid:
1q = load "0Fbxx000000002qCAA/0Fcxx000000002WCAQ";
2q = group q by 'FirstName';
3q = foreach q generate 'FirstName' as 'User_FirstName', sum('mea_mm10M') as 'sum_mm10M';
4q = order q by 'User_FirstName' desc;