Subscribe for updates on posts
Be the first to read the latest news

A-Z 0-9 custom sorting in Lucene

May 17th, 2007 by Ioan Cocan in Java, General

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;

You might also like

Hibernate Annotations 3.2.1 – Lucene sort Keeping database and Lucene index in sync is easily done using the new Lucene-Hibernate integration. Just...
Problem when adding elements to a TreeSet<Comparator> – some elements are not added I thought of sharing with you this little problem that I encountered when using a TreeSet to display...
Migrate Lucene annotations 3.2.1GA to Hibernate Search 3.0.0.Beta1: object removal Changes in Hibernate Search are quite frequent, as it is in continued development. Today I found myself...
How to order by a custom SQL formula/expression when using hibernate Criteria API In our current project we are using Spring + Hibernate Annotations. Today I needed to use something...

3 Responses

  1. Pavan Says:

    Thanks a lot for the code.. It was really useful in Custom Sort Implementation

  2. Dmitry Says:

    The code is a good one.
    But since then a lot has changed.

    Now I’m trying to implement custom sorting and it doesn’t work with FieldComparatorSource on multiple shards (I’m actually using Solr and need to have sort order which is different from the natural one for field values).

  3. Lucene exact ordering « « Programmers Goodies Programmers Goodies Says:

    […] suggest you separate the search from the sort and use a CustomSorter for the sorting: Here’s a blog entry describing a custom sorter. The classic Lucene book describes this […]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.