A-Z 0-9 custom sorting in Lucene

1 min read >

A-Z 0-9 custom sorting in Lucene

Engineering Insights & Enterprise solutions

The natural sort order for String fields is 0-9 A-Z so it seems a custom sorter is needed. I guess this is a normal request for any application that does a little sorting so I am surprised nothing came up during a search. Here is the AZ09Comparator:

/**
 * Custom Lucene sorting A-Z 0-9
 *
 * @author icocan
 */
public class AZ09Comparator implements SortComparatorSource {

    private static final Logger logger = Logger.getLogger(AZ09Comparator.class);

    public ScoreDocComparator newComparator(final IndexReader indexReader, final String str) throws IOException {
        return new ScoreDocComparator() {
            public int compare(ScoreDoc scoreDoc1, ScoreDoc scoreDoc2) {
                try {
                    final Document doc1 = indexReader.document(scoreDoc1.doc);
                    final Document doc2 = indexReader.document(scoreDoc2.doc);
                    final String strVal1 = doc1.get(str);
                    final String strVal2 = doc2.get(str);

                    boolean c1 = Character.isDigit(strVal1.charAt(0));
                    boolean c2 = Character.isDigit(strVal2.charAt(0));

                    if (c1 && c2) {
                        return strVal1.compareTo(strVal2);
                    } else if (c1) {
                        return 1;
                    } else if (c2) {
                        return -1;
                    } else {
                        return strVal1.compareTo(strVal2);
                    }
                } catch (IOException e) {
                    logger.error("Cannot read doc", e);
                }
                return 0;
            }

            public Comparable sortValue(ScoreDoc scoreDoc) {
                return new Float(scoreDoc.doc);
            }

            public int sortType() {
                return SortField.CUSTOM;
            }
        };
    }
}

Unlock innovation and digital transformation for your business.

Get in touch

We are always happy to talk

Phone

+40-21-223-7700

Email

hello@tremend.com

Address

165 Splaiul Unirii, Timpuri Noi Square,
TN Office 2 building, 4th floor,
District 3, Bucharest, Romania, 030134