@@ -210,47 +210,49 @@ $(function() {
210
210
selectdata . push ( { id : item . toLowerCase ( ) , fullText : item , text : item . toLowerCase ( ) , kind : match [ 1 ] } ) ;
211
211
}
212
212
}
213
-
214
213
$ ( '.select2' ) . select2 ( {
215
214
placeholder : "Search" ,
216
215
minimumInputLength : 1 ,
217
216
formatInputTooShort : '' ,
218
217
data : selectdata ,
219
218
formatResult : format ,
220
- sortResults : function ( results , container , query ) {
221
- if ( ! query . term ) {
222
- return results ;
223
- }
224
-
225
- var term = query . term . toLowerCase ( ) ;
226
-
227
- // order two items by the location of the search term in the text
228
- // that is, when the term is "string", String should match before StaticString
229
- var orderByOffset = function ( a , b ) {
230
- var indexOffset = a . indexOf ( term ) - a . indexOf ( term ) ;
231
-
232
- // if the relative offset of the search term is nonzero, the term
233
- // with the lower offset should come first
234
- if ( indexOffset != 0 ) {
235
- return indexOffset ;
236
- }
237
-
238
- // otherwise, order using case-insensitive alpha
239
- if ( a == b ) {
240
- return 0 ;
241
- } else {
242
- return ( a < b ) ? - 1 : 1 ;
243
- }
219
+ sortResults : function ( results , container , query ) {
220
+ if ( query . term ) {
221
+ var term = query . term . toLowerCase ( ) ;
222
+ return results . sort ( function ( a , b ) {
223
+ // we sort first by location of the search term in the matches
224
+ // that is, when the term is "string", String should match before StaticString
225
+ // to begin, find the relative search term offset of the two matches
226
+ var aComp = a . text || a . id ;
227
+ var bComp = b . text || b . id ;
228
+ var indexOffset = aComp . indexOf ( term ) - bComp . indexOf ( term ) ;
229
+
230
+ // if the relative offset of the search term is nonzero, the term
231
+ // with the lower offset should come first
232
+ if ( indexOffset != 0 ) {
233
+ return indexOffset ;
234
+
235
+ // otherwise, order using case-insensitive alpha
236
+ } else {
237
+ if ( aComp != bComp ) {
238
+ return ( aComp < bComp ) ? - 1 : 1 ;
239
+ } else {
240
+ // if the two terms are the same, expand the comparison to look at their fully qualified names
241
+ indexOffset = a . id . indexOf ( term ) - b . id . indexOf ( term ) ;
242
+ if ( indexOffset != 0 ) {
243
+ return indexOffset ;
244
+ } else {
245
+ if ( a . id == b . id ) {
246
+ return 0 ;
247
+ } else {
248
+ return ( a . id < b . id ) ? - 1 : 1 ;
249
+ }
250
+ }
251
+ }
252
+ }
253
+ } ) ;
244
254
}
245
-
246
- return results . sort ( function ( a , b ) {
247
- var compResult = orderByOffset ( a . text || a . id , b . text || b . id ) ;
248
- if ( compResult != 0 ) {
249
- return compResult ;
250
- }
251
-
252
- return orderByOffset ( a . id , b . id ) ;
253
- } ) ;
255
+ return results ;
254
256
}
255
257
} )
256
258
. on ( "change" , function ( e ) {
0 commit comments