Created
October 10, 2014 11:10
-
-
Save Palmr/c265497fa506061fdd97 to your computer and use it in GitHub Desktop.
Sort tags by suggestion text value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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