You can sort by function, see: 
https://lucene.apache.org/solr/guide/6_6/function-queries.html. Not quite sure 
what the function would look like though.

What I would do rather than a custom sort or sort by function is normalize 
these into a different field at index time and just sort on that. You might be 
able to do that with a copyField and clever tokenization, or you could populate 
the field during the ETL process.

The normalized field would have to be clever enough to prefix pure numerics 
with a bunch of zeros though, something like zzzzz00000000000000001 otherwise 
you’d be sorting the numerics lexically, and 100 would come before 2.

A third alternative is to populate two separate fields, one numeric and one 
string then specify primary and secondary fields rather than one field, 
something like
&sort string_field asc, numeric_field asc
You’d have to pay attention to sortMissingFirst/Last on the string field, you’d 
probably want sortMissingLast.

Best,
Erick


> On Aug 7, 2020, at 12:47 PM, Pushkar Raste <pushkar.ra...@gmail.com> wrote:
> 
> Hi,
> Is it possible to add a custom comparator to a field for sorting. e.g.
> let's say I have field 'name' and following documents
> 
> {
>  id : "doc1",
>  name : "1"
> }
> 
> 
> {
>  id : "doc2",
>  name : "S1"
> }
> 
> 
> {
>  id : "doc2",
>  name : "S2"
> }
> 
> if I sort using field 'name', the order would be : ["doc1", "doc2", "doc3"]
> but I want pure numbers to last and want the order ["doc2", "doc3",
> "doc1"]. Is there a way I can provide my own comparator?

Reply via email to