org.mongodb.morphia.query.Sort Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of morphia Show documentation
Show all versions of morphia Show documentation
Java Object Document Mapper for MongoDB
The newest version!
package org.mongodb.morphia.query;
/**
* Used for sorting query results or defining a sort stage in an aggregation pipeline
*
* @mongodb.driver.manual reference/operator/aggregation/sort/ $sort
* @since 1.3
*/
public class Sort {
private static final String NATURAL = "$natural";
private final String field;
private final int order;
/**
* Creates a sort on a field with a direction.
*
* - 1 to specify ascending order.
* - -1 to specify descending order.
*
*
* @param field the field
* @param order the order
*/
protected Sort(final String field, final int order) {
this.field = field;
this.order = order;
}
/**
* Creates an ascending sort on a field
*
* @param field the field
* @return the Sort instance
*/
public static Sort ascending(final String field) {
return new Sort(field, 1);
}
/**
* Creates a descending sort on a field
*
* @param field the field
* @return the Sort instance
*/
public static Sort descending(final String field) {
return new Sort(field, -1);
}
/**
* Creates an ascending sort on a field
*
* @return the Sort instance
*/
public static Sort naturalAscending() {
return new Sort(NATURAL, 1);
}
/**
* Creates a descending natural sort on a field
*
* @return the Sort instance
*/
public static Sort naturalDescending() {
return new Sort(NATURAL, -1);
}
/**
* Returns the sort order.
*
* - 1 for ascending order.
* - -1 for descending order.
*
*
* @return the sort order
*/
public int getOrder() {
return order;
}
/**
* @return the sort field
*/
public String getField() {
return field;
}
}