6

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.

4

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.load(items);
   ctx.executeQueryAsync(function() {
       success(items);
   },error);
}


//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;
}



getListItems('Tasks',
      function(items)
      {
           //sort list items 
           items.get_data().sort(itemComparer); //sort items by ID (DESC)
           items.get_data().forEach(function(item){
              console.log(item.get_item('ID')); 
           });

      },
      function(sender,args){
         console.log(args.get_message());
     });

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 May 4 '15 at 13:40
3

You can use the Order By in CAML:

<OrderBy>
 <FieldRef Name = "ID" Ascending = "TRUE"/>
</OrderBy>
<Where>
   ...
</Where>
  • It is my understanding that he didn't want to execute another caml query... is it possible to use caml in this scenario? – BlueBird May 4 '15 at 13:20
  • @BennySkogberg what would be a way to apply your CAML code to an already returned list items collection? – Boris May 4 '15 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 May 4 '15 at 13:42
1

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 May 4 '15 at 13:27
  • @BlueBird, yeah sorry bro, only JSOM would work for me. – Boris May 4 '15 at 13:36
  • 1
    good point... my bad. – BlueBird May 4 '15 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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