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