I am using SP.CamlQuery.createAllItemsQuery() to grab all items from a custom list in SharePoint, however even though the list itself is sorted in an alphabetical order, when it comes through to a select tag in the html webpart it comes in the order the items were added to the list (I assume it's sorted by ID) How can I make it sort alphabetically to make the list easier to read for the user?
If you want the data to be retrieved in specific order then you cannot use createAllItemsquery as it does not create query based on any existing views on the list. Instead you should include a caml query with specific sort order.
a) To get items based on existing view on a list, please refer to this answer
b) To retrieve items based on specific query use below example. Note the order by clause is included to sort items by modified date.
var query = <View Scope='RecursiveAll'> <Query> <Where> </Where> <OrderBy> <FieldRef Name='Modified' Ascending='False' /> </OrderBy> </Query> </View> var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(query); allItems = list.getItems(camlQuery); context.load(allItems); context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
c) You can always sort the items after retrival. You can refer to the answer of this question for an example