SharePoint/PnP-JS-Core

Order By is not working properly in PnP JS Core

Closed this issue ยท 5 comments

Hi,
I am using the below PnP code to sort the items but it is not sorting. Please help me to resolve this.

const sortField = 'Title';
const sortAsc = false;
let items = sp.web.lists.getByTitle(this.listTitle).items
.filter(filterQuery)
.select('*', 'Author/ID', 'Author/Title', 'Editor/ID', 'Editor/Title')
.expand('Author,Editor')
.orderBy(sortField, sortAsc);
if (sortField !== 'ID') { items = items.orderBy('ID', false); } items = items.top(pageSize); return Observable.fromPromise(items.getPaged()) .map(res => { res.results = res.results.map(r => this.castAsReportingValue(r)); return res; });

From the above code, if I set the "sortField" to "Created" column it is working fine. For Title field it is not working. Kindly share your inputs.

Are you getting an error back? Some fields are not sortable and perhaps in the list you are accessing Title is not?

Hi Rodgers,
I am not getting any error but it is not sorting. The results are displayed their own sorts not by ID or not by Title also.
Regards,
Philip

Hey Philip,

It's more likely something wrong with the code on your end.
No one will argue that pagination in SP is a pain, but the getPaged method works well for me as it receives and uses skipToken prepared by API backend including edge values of the ordered by fields. For instance, .skip in this situation would return unpredictable sets of data while ordering by a field with non-unique values.

The image below shows paged results with filtering and ordering conditions:

image

Hi Andrew,
It is working fine now. Thanks for shared the solution with good explanations and screenshots.

Nice! Glad you figured out. Closing the issue.