Custom ordering of results

1,055 views
Skip to first unread message

Jonah Greenthal

unread,
Jul 22, 2015, 11:53:27 AM7/22/15
to select2
I'd like to reorder my search results (on the client side) to prioritize ones that start with the search text, while still displaying ones that don't. How can I do that?

(This seems related, but not identical, to https://groups.google.com/forum/#!searchin/select2/sorting/select2/oPZMI6lbSzM/W1uDZR_JnpwJ and https://github.com/select2/select2/issues/3106.)

Thanks!

Jonah Greenthal

unread,
Jul 27, 2015, 12:32:38 PM7/27/15
to select2, jonahgr...@gmail.com
Anyone? Please?

Kevin Brown

unread,
Jul 30, 2015, 12:42:19 PM7/30/15
to select2, jonahgr...@gmail.com
Sorry about the delay, I can only answer half of your question.

Select2 provides the `sorter` option (previously called `sortResults`) which takes in the array of results to be displayed and expects a sorted array to be returned as a result. Select2, by default, does not do any special sorting and just passes the original array of results through. You can override the `sorter` method (it's a function) when you initialize Select2.

You can find an example in the old documentation (change `sortResults` to `sorter`): http://select2.github.io/select2/#sort_results
Also note that only the first parameter (`results`) is supported in Select2 4.0.0.

The two links you provided were about ordering the selection, not the results.

--
You received this message because you are subscribed to the Google Groups "select2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
To post to this group, send email to sel...@googlegroups.com.
Visit this group at http://groups.google.com/group/select2.
For more options, visit https://groups.google.com/d/optout.

Jonah Greenthal

unread,
Jul 30, 2015, 1:11:12 PM7/30/15
to Kevin Brown, select2
Thanks! Is there documentation on the object types for the arguments?

Jonah Greenthal

unread,
Jul 30, 2015, 1:47:01 PM7/30/15
to Kevin Brown, select2
Okay, I think I figured that out -- but how do I access the query,
given that only the results parameter is supported in v4?

Thanks,
Jonah

Kevin Brown

unread,
Jul 30, 2015, 9:20:28 PM7/30/15
to Jonah Greenthal, select2
Because Select2 4.0.0 separates the retrieval of results from the display of them, the `sorter` method can't provide the context for the query (and as a result, the search term). You can intercept this in other areas and store it in a global variable though.

You can find an example here on this Stack Overflow answer for underlining term matches in search results: http://stackoverflow.com/q/28958620/359284

Basically you need to override `language.searching` (which takes in `query`) such that it stores the argument to a global variable and returns the "Searching..." text you would normally expect. You can guarantee that the searching translation will be called before the results are sorted and rendered.

Jonah Greenthal

unread,
Jul 30, 2015, 11:55:32 PM7/30/15
to Kevin Brown, select2
Thanks!
Reply all
Reply to author
Forward
0 new messages