@@ -33,6 +33,9 @@ import org.locationtech.geomesa.core.util.CloseableIterator._
33
33
import org .locationtech .geomesa .core .util .{CloseableIterator , SelfClosingIterator }
34
34
import org .locationtech .geomesa .utils .geotools .SimpleFeatureTypes
35
35
import org .opengis .feature .simple .{SimpleFeature , SimpleFeatureType }
36
+ import org .opengis .filter .sort .{SortBy , SortOrder }
37
+
38
+ import scala .reflect .ClassTag
36
39
37
40
object QueryPlanner {
38
41
val iteratorPriority_RowRegex = 0
@@ -180,10 +183,37 @@ case class QueryPlanner(schema: String,
180
183
DensityIterator .expandFeature(decoder.decode(kv.getValue))
181
184
}
182
185
} else {
183
- accumuloIterator.map { kv => decoder.decode(kv.getValue) }
186
+ val features = accumuloIterator.map { kv => decoder.decode(kv.getValue) }
187
+ if (query.getSortBy != null && query.getSortBy.length > 0 ) sort(features, query.getSortBy)
188
+ else features
189
+ }
190
+ }
191
+
192
+ private def sort (features : CloseableIterator [SimpleFeature ],
193
+ sortBy : Array [SortBy ]): CloseableIterator [SimpleFeature ] = {
194
+ val sortOrdering = sortBy.map {
195
+ case SortBy .NATURAL_ORDER => Ordering .by[SimpleFeature , String ](_.getID)
196
+ case SortBy .REVERSE_ORDER => Ordering .by[SimpleFeature , String ](_.getID).reverse
197
+ case sb =>
198
+ val prop = sb.getPropertyName.getPropertyName
199
+ val ord = attributeToComparable(prop)
200
+ if (sb.getSortOrder == SortOrder .DESCENDING ) ord.reverse
201
+ else ord
184
202
}
203
+ val comp : (SimpleFeature , SimpleFeature ) => Boolean =
204
+ if (sortOrdering.length == 1 ) {
205
+ // optimized case for one ordering
206
+ val ret = sortOrdering.head
207
+ (l, r) => ret.compare(l, r) < 0
208
+ } else {
209
+ (l, r) => sortOrdering.map(_.compare(l, r)).find(_ != 0 ).getOrElse(0 ) < 0
210
+ }
211
+ CloseableIterator (features.toList.sortWith(comp).iterator)
185
212
}
186
213
214
+ def attributeToComparable [T <: Comparable [T ]](prop : String )(implicit ct : ClassTag [T ]): Ordering [SimpleFeature ] =
215
+ Ordering .by[SimpleFeature , T ](_.getAttribute(prop).asInstanceOf [T ])
216
+
187
217
// This function calculates the SimpleFeatureType of the returned SFs.
188
218
private def getReturnSFT (query : Query ): SimpleFeatureType =
189
219
query match {
0 commit comments