From 92617ec54ec310144732ff6f2e35e113fd891ab9 Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Wed, 8 May 2013 23:38:11 -0700 Subject: [PATCH] preserve order of elements in the data call. fixes #1280 --- select2.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/select2.js b/select2.js index 17253033..1cd63864 100644 --- a/select2.js +++ b/select2.js @@ -2236,7 +2236,21 @@ the specific language governing permissions and limitations under the Apache Lic return is_match; }, callback: !$.isFunction(callback) ? $.noop : function() { - callback(matches); + // reorder matches based on the order they appear in the ids array because right now + // they are in the order in which they appear in data array + var ordered = []; + for (var i = 0; i < ids.length; i++) { + var id = ids[i]; + for (var j = 0; j < matches.length; j++) { + var match = matches[j]; + if (equal(id, opts.id(match))) { + ordered.push(match); + matches.splice(j, 1); + break; + } + } + } + callback(ordered); } }); };