prefuse.data.util
Class Sort

java.lang.Object
  extended by prefuse.data.util.Sort

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.

Author:
jeffrey heer

Constructor Summary
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.
 
Method Summary
 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()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Sort

public Sort()
Creates a new, empty Sort specification.


Sort

public Sort(java.lang.String[] fields)
Creates a new Sort specification that sorts on the given fields, all in ascending order.

Parameters:
fields - the fields to sort on, in order of precedence

Sort

public Sort(java.lang.String[] fields,
            boolean[] ascend)
Creates a new Sort specification that sorts on the given fields in the given orders.

Parameters:
fields - the fields to sort on, in order of precedence
ascend - for each field, indicates if the field should be sorted in ascending (true) or descending (false) order
Method Detail

add

public void add(java.lang.String field,
                boolean ascend)
Adds a new field to this Sort specification.

Parameters:
field - the additional field to sort on
ascend - indicates if the field should be sorted in ascending (true) or descending (false) order

size

public int size()
Returns the number of fields in this Sort specification.

Returns:
the number of fields to sort on

getField

public java.lang.String getField(int i)
Returns the sort field at the given index.

Parameters:
i - the index to look up
Returns:
the sort field at the given index

isAscending

public boolean isAscending(int i)
Returns the ascending modifier as the given index.

Parameters:
i - the index to look up
Returns:
true if the field at the given index is to be sorted in ascending order, false for descending order

getComparator

public java.util.Comparator getComparator(TupleSet ts)
Generates a Comparator to be used for sorting tuples drawn from the given tuple set.

Parameters:
ts - the TupleSet whose Tuples are to be sorted
Returns:
a Comparator instance for sorting tuples from the given set using the sorting criteria given in this specification

parse

public 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. 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.

Parameters:
s - the sort specification String
Returns:
a new Sort specification

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()


Copyright © 2007 Regents of the University of California