I am fetching a collection of items from a list using a CAML query. The returned items are then populated in my custom HMTL table. There is, however, another HTML table for which I need the same collection of items, but sorted differently.

Is there any way to get the same items collection sorted differently without having to execute another query over the same list?

I am required to use JSOM, so C# LINQ and such won't work for me.

3 Answers 3


You could sort list items using array.sort function:

function getListItems(listTitle,success,error)
   var ctx = SP.ClientContext.get_current();
   var list = ctx.get_web().get_lists().getByTitle(listTitle);
   var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
   ctx.executeQueryAsync(function() {

//sort List Items By ID (DESC)
function itemComparer(x,y) {
    var key = 'ID';
  if (x.get_item(key) < y.get_item(key))
     return 1;
  if (x.get_item(key) > y.get_item(key))
    return -1;
  return 0;

           //sort list items 
           items.get_data().sort(itemComparer); //sort items by ID (DESC)


How to measure time taken by a function to execute

var start = new Date().getTime();
items.get_data().sort(itemComparer); //sort items by title
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);
  • your solutions is the only one I see feasible. Have you had any experience with the sort method performance? I will be sorting up to 1000 list items. Thanks!
    – Boris
    Commented May 4, 2015 at 13:40

You can use the Order By in CAML:

 <FieldRef Name = "ID" Ascending = "TRUE"/>
  • It is my understanding that he didn't want to execute another caml query... is it possible to use caml in this scenario?
    – BlueBird
    Commented May 4, 2015 at 13:20
  • @BennySkogberg what would be a way to apply your CAML code to an already returned list items collection?
    – Boris
    Commented May 4, 2015 at 13:38
  • @Boris Since your using JSOM I guess you can't save the result in a variable for later second use, so I'd guess you need two CAML queries in this case.
    – Benny Skogberg
    Commented May 4, 2015 at 13:42

Could you use linq?:

SPList objRelationShipList;
SPListItemCollection objRelationShipListItems;
objRelationShipList = SPContext.Current.Web.Lists["ListName"];
objRelationShipListItems = objRelationShipList.Items;
List<SPListItem> results = (from SPListItem item in objRelationShipListItems           
                            orderby item.ID
                            select item).ToList();
foreach(SPListItem in results)

Ref: http://blog.techperspect.com/2011/05/sorting-splistitemcollection-using-linq.html

  • 2
    I thought he couldn't use LINQ either. "I am required to use JSOM, so C# LINQ and such won't work for me."
    – Benny Skogberg
    Commented May 4, 2015 at 13:27
  • @BlueBird, yeah sorry bro, only JSOM would work for me.
    – Boris
    Commented May 4, 2015 at 13:36
  • 1
    good point... my bad.
    – BlueBird
    Commented May 4, 2015 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.