public class Sort
extends java.lang.Object
Utility class representing sorting criteria, this can be given as
input to the TupleSet.tuples(Predicate, Sort)
method to
get a sorted iteration of tuples.
Sort criteria consists of an ordered list of data field names to
sort by, along with an indication to sort tuples in either ascending
or descending order. These criteria can be passed in to the
constructor or added incrementally using the
add(String, boolean)
method.
Alternatively, one can also specify the sorting criteria using a
single string, which is parsed using the parse(String)
method.
This string should consist
of a comma-delimited list of field names, which optional "ASC" or
"DESC" modifiers to specify ascending or descending sorts. If no
modifier is given, ascending order is assumed. Field
names which include spaces or other non-standard characters should
be written in brackets ([]), just as is done in
expression
language statements
. For example, the
following string
"Profit DESC, [Product Type]"
sorts first by the data field "Profit" in descending order, additionally sorting in ascending order by the data field "Product Type" for tuples which have identical values in the "Profit" field.
Constructor and Description |
---|
Sort()
Creates a new, empty Sort specification.
|
Sort(java.lang.String[] fields)
Creates a new Sort specification that sorts on the
given fields, all in ascending order.
|
Sort(java.lang.String[] fields,
boolean[] ascend)
Creates a new Sort specification that sorts on the
given fields in the given orders.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.String field,
boolean ascend)
Adds a new field to this Sort specification.
|
java.util.Comparator |
getComparator(TupleSet ts)
Generates a Comparator to be used for sorting tuples drawn from
the given tuple set.
|
java.lang.String |
getField(int i)
Returns the sort field at the given index.
|
boolean |
isAscending(int i)
Returns the ascending modifier as the given index.
|
static Sort |
parse(java.lang.String s)
Parse a comma-delimited String of data fields to sort on, along
with optional ASC or DESC modifiers, to generate a new Sort
specification.
|
int |
size()
Returns the number of fields in this Sort specification.
|
java.lang.String |
toString() |
public Sort()
public Sort(java.lang.String[] fields)
fields
- the fields to sort on, in order of precedencepublic Sort(java.lang.String[] fields, boolean[] ascend)
fields
- the fields to sort on, in order of precedenceascend
- for each field, indicates if the field should
be sorted in ascending (true) or descending (false) orderpublic void add(java.lang.String field, boolean ascend)
field
- the additional field to sort onascend
- indicates if the field should
be sorted in ascending (true) or descending (false) orderpublic int size()
public java.lang.String getField(int i)
i
- the index to look uppublic boolean isAscending(int i)
i
- the index to look uppublic java.util.Comparator getComparator(TupleSet ts)
ts
- the TupleSet whose Tuples are to be sortedpublic static Sort parse(java.lang.String s)
expression
language statements
. For example, the
following string
"Profit DESC, [Product Type]"
sorts first by the data field "Profit" in descending order, additionally sorting in ascending order by the data field "Product Type" for tuples which have identical values in the "Profit" field.
s
- the sort specification Stringpublic java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
Copyright ? 2013 Regents of the University of California