function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aruna Anala 6Aruna Anala 6 

How to perform sorting on object of array objects

Eg:
const obj ={
  x:[
    {
      name:"Aruna",
      Position:"3"
    },
    {
      name:"Sri",
      Position:"1"
      }
]
};

How should i perform sorting based on the position in javascript?
Any body please help me with this ...
Malika Pathak 9Malika Pathak 9
Hi Aruna
This might help you

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
ravi soniravi soni
Hi Aruna,
try following code and check output that it's working according to you or not.
const obj ={
  x:[
    {
      name:"Aruna",
      Position:"3"
    },
    {
      name:"Sri",
      Position:"1"
      }
]
};
function compare_Position(a, b){
        if(a.Position < b.Position){
                return -1;
        }else if(a.Position > b.Position){
                return 1;
        }else{
                return 0;
        }
}
var sortingDataLst = [];
for(let sObj of obj.x){
sortingDataLst.push(sObj);
}
 var dataBasedOnQty = sortingDataLst.sort(compare_Position);
 console.log(dataBasedOnQty);

/*Output : 0: {name: "Sri", Position: "1"}
1: {name: "Aruna", Position: "3"}
*/
Let me know if it helps you and marking it as best.
Thank you