Skip to content

Instantly share code, notes, and snippets.

@Palmr
Created October 10, 2014 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Palmr/c265497fa506061fdd97 to your computer and use it in GitHub Desktop.
Save Palmr/c265497fa506061fdd97 to your computer and use it in GitHub Desktop.
Sort tags by suggestion text value
// This is whatever has your JSON before you call the tagger
var tagItemsJSON = ...
// This takes the JSON and splits it into an array of the items in it
// It then sorts that array by the suggestion field on each item
var sortedBySuggestion = $.map(tagItemsJSON, function(p){return p;}).sort(function(a, b){
if(a.suggestion < b.suggestion ) return -1;
if(a.suggestion > b.suggestion ) return 1;
return 0;
});
// Then loop over the tag items, no sorted by suggestion
for (var i = 0; i < sortedBySuggestion.length; i++) {
// For each item in the array, the index it's at should be the sort field on the original tag items JSON
tagItemsJSON[sortedBySuggestion[i].id].sort = i;
}
// Then call the tagger
$('#selectorElement').tagger({
availableTags: tagItemsJSON
, sortedOutput: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment