01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.solr.search;
17:
18: import org.apache.lucene.search.*;
19:
20: /**
21: * Extra lucene sorting utilities & convenience methods
22: *
23: * @author yonik
24: * @version $Id: Sorting.java 472574 2006-11-08 18:25:52Z yonik $
25: *
26: */
27:
28: public class Sorting {
29:
30: /** Returns a {@link SortField} for a string field.
31: * If nullLast and nullFirst are both false, then default lucene string sorting is used where
32: * null strings sort first in an ascending sort, and last in a descending sort.
33: *
34: * @param fieldName the name of the field to sort on
35: * @param reverse true for a reverse (desc) sort
36: * @param nullLast true if null should come last, regardless of sort order
37: * @param nullFirst true if null should come first, regardless of sort order
38: * @return SortField
39: */
40: public static SortField getStringSortField(String fieldName,
41: boolean reverse, boolean nullLast, boolean nullFirst) {
42: if (nullLast) {
43: if (!reverse)
44: return new SortField(fieldName,
45: nullStringLastComparatorSource);
46: else
47: return new SortField(fieldName, SortField.STRING, true);
48: } else if (nullFirst) {
49: if (reverse)
50: return new SortField(fieldName,
51: nullStringLastComparatorSource, true);
52: else
53: return new SortField(fieldName, SortField.STRING, false);
54: } else {
55: return new SortField(fieldName, SortField.STRING, reverse);
56: }
57: }
58:
59: static final SortComparatorSource nullStringLastComparatorSource = new MissingStringLastComparatorSource();
60: }
|