lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul J. Lucas <p...@lucasmail.org>
Subject Re: Sorting case-insensitively
Date Thu, 03 Jul 2008 22:25:59 GMT
On Jun 30, 2008, at 8:08 PM, Paul J. Lucas wrote:

> On Jun 30, 2008, at 8:55 PM, Paul J. Lucas wrote:
>> If I have a SortField with a type of STRING, is there any way to  
>> sort in a case-insensitive manner?
>
> Couldn't I also use a custom SortComparator?

OK, so I tried that by doing:

	SortField sortField = new SortField(
	    sortFieldName,
	    new CaseInsensitiveStringSortComparator(), descending
	);

where CaseInsensitiveStringSortComparator is:

	class CaseInsensitiveStringSortComparator extends SortComparator {
	    public Comparable getComparable( String termText ) {
	        return new CaseInsensitiveStringComparable( termText );
	    }
	}

and CaseInsensitiveStringComparable is:

	public class CaseInsensitiveStringComparable implements Comparable {
	
	    public CaseInsensitiveStringComparable( String s ) {
	        m_string = s.toLowerCase();
	    }
	
	    public int compareTo( Object o ) {
	        final String s;
	        if ( o instanceof CaseInsensitiveStringComparable )
	            s = ((CaseInsensitiveStringComparable)o).m_string;
	        else if ( o instanceof String )
	            s = ((String)o).toLowerCase();
	        else
	            throw new ClassCastException();
	        return m_string.compareTo( s );
	    }
	
	    public boolean equals( Object o ) {
	        if ( !(o instanceof CaseInsensitiveStringComparable ) )
	            return false;
	        final CaseInsensitiveStringComparable c =
	            (CaseInsensitiveStringComparable)o;
	        return m_string.equals( c.m_string );
	    }
	
	    public int hashCode() {
	        return m_string.hashCode();
	    }
	
	    private final String m_string;
	}

But when my code runs, I get a NullPointerException in Lucene's  
SortComparator,
line 54 which reads:

	return cachedValues[i.doc].compareTo (cachedValues[j.doc]);

because cachedCalues[i.doc] is null.  But why is it null?

- Paul

P.S.: Im using Lucene 2.3.2.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message