Skip to content

Commit 92617ec

Browse files
committed
preserve order of elements in the data call. fixes select2#1280
1 parent 3d4595c commit 92617ec

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

select2.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,21 @@ the specific language governing permissions and limitations under the Apache Lic
22362236
return is_match;
22372237
},
22382238
callback: !$.isFunction(callback) ? $.noop : function() {
2239-
callback(matches);
2239+
// reorder matches based on the order they appear in the ids array because right now
2240+
// they are in the order in which they appear in data array
2241+
var ordered = [];
2242+
for (var i = 0; i < ids.length; i++) {
2243+
var id = ids[i];
2244+
for (var j = 0; j < matches.length; j++) {
2245+
var match = matches[j];
2246+
if (equal(id, opts.id(match))) {
2247+
ordered.push(match);
2248+
matches.splice(j, 1);
2249+
break;
2250+
}
2251+
}
2252+
}
2253+
callback(ordered);
22402254
}
22412255
});
22422256
};

0 commit comments

Comments
 (0)