Sorting a List array based on an attribute of the List items

bammar
Kilo Sage
Kilo Sage

I have a list of items our users can select. I want to sort it based on an attribute ( another field , not the primary label people see in the list bucket) . Then i want to evaluate and run code on the array results in order based of an attribute of the list item they select. Right now by default the system is running my script based on Alphabetical order of the Lables of the array items in the list. Thank you in Advance

Basically its sorting on TASK NAME ( the field people see ) but there is another field called ORDER (u_order) that i want the array to sort by before evaluating the items one by one...

var list = current.u_select_action_plan_tasks.toString();

var array= list.split(",");

//I believe I want to sort by Order here at this point before it starts going through the array...

//help needed here

for (var i=0; i < array.length; i++) {

DO SOMETHING....

3 REPLIES 3

Chuck Tomasi
Tera Patron

It sounds like you want to build an array of objects. Each object would have a property for the display value (task number) and order (to sort on.) Correct?



I'm going to also assume that u_select_action_plan_tasks is a list variable with multiple sys_ids in it.



If that's the case you can build your objects something like this. (Note, this code is not tested and requires modifications to match your tables/fields/etc.)



var taskObjList = [];



var taskIds = current.u_select_action_plan_tasks.split(',');



for (i = 0; i < taskIds.length; i++) {


        var task = new GlideRecord('task');


        task.get(taskIds[i]);


        var tmpObj = {};


        tmpObj.number = task.getValue('number');


        tmpObj.sys_id = task.getValue('sys_id'); // Used for later reference


        tmpObj.order = task.getValue('order'); // this may be u_order, check your instance


        taskObjList.push(tmpObj);


}



var newList = tmpObjList.sort(sortByOrder);



function sortByOrder() {


        // This is where you sort based on order - reference: Sort array of objects by string property value in JavaScript - Stack Overflow


}


Thank you Chuck,



I will try to integrate your code and see what happens. I checked that page as well from stackoverflow, the issue with their code is they are always defining the arrays in the code so I couldn't see a 1 to 1 application of their methods.




Bessam Ammar


Platform Analyst - IT


Florida Virtual School


2145 MetroCenter Blvd, Suite 200


Orlando, FL 32825


Office: (407) 513-3666


Email: bammar@flvs.net<mailto:bammar@flvs.net>


Web: www.flvs.net



Please Consider the environment before printing this email.


The only difference between what they did and what I did is they hard coded their array and I constructed mine from database records. Sorting should still work the same in the compare function.