PnPJs SPFx orderBy() not working?

Pexels Andrea Piacquadio 3822850
Photo by Andrea Piacquadio

In the PnPJs Agnostic Framework Some fields are not sortable, let’s assume this call is using a MODEL where TrainingAreaPosition is a number column without decimal places:

const [trainingPages, setPages] = useState<ISomeModel[]>([]);

//* Omitted for abbreviation */

const spCache = spfi(_sp).using(Caching({ store: "session" }))
const sortField: string = "TrainingAreaPosition";
const sortAsc: boolean = true;

const response: ISomeModelResponse[] = await spCache.web.lists
          "Id", "Title", "TrainingArea", "TrainingPageActive", "TrainingAreaPosition", "FileLeafRef")
                    .filter("TrainingPageActive eq 1")
                    .orderBy(sortField, sortAsc)

const items: ISomeModel[] = ISomeModelResponse) => {
    return {
        Id: item.Id,
        Title: item.Title,
        TrainingArea: item.TrainingArea,
        Active: item.TrainingPageActive,
        Position: item.TrainingAreaPosition,
        Name: item.FileLeafRef


I’m using a Custom React Hook, fix by comparing the field name, where Position is a property of the Model to Map

const byPosition = items.slice(0);
byPosition.sort(function (a, b) {
    return a.Position - b.Position;


//* Omitted for abbreviation */

    return [trainingPages, isError] as const

export default useTrainingPages

Joao Livio

João has over two decades of IT experience and holds several certifications, including Microsoft Certified Professional (MCP) and Microsoft Certified Technology Specialist (MCTS). He is also a Lean Specialist with expertise in Lean Management, Nintex, and other training programs from LinkedIn and Udemy, including exams related to Azure and the Power Platform. As a Consultant and Advisor for Modern Work, João assists clients in unlocking their digital potential and optimizing productivity and collaboration. His approach is agnostic to technology, focusing on practical solutions that align with business goals. He has consistently been recognized as a Microsoft Most Valuable Professional (MVP) for 10 consecutive years. His commitment to excellence extends beyond technical expertise; he also values consistency, mental health, creative thinking, teamwork, and fun at work. João believes that learning every day within a team context is the key to continuous improvement.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *