cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Sorting an infotable based on order specified

polasaikrishna
6-Contributor

Sorting an infotable based on order specified

Hi,

 

I am trying to sort an infotable result set based on specific order (array)

 

Example:

My Infotable result set:

Column1 Column2 Column3
d desc1 desc one
h desc2 desc two
f desc3 desc three
t desc4 desc four

 

My order to sort  ::  [f,t,h,d]

 

Expected result set after sorting

Column1 Column2 Column3
f desc3 desc three
t desc4 desc four
h desc2 desc two
d desc1 desc one

 

Would highly appreciate a quick turnaround.

 

Thanks in advance.

ACCEPTED SOLUTION

Accepted Solutions

Not super efficient, but should work:

 

function sort(array,sortOrder,sortColumn) {
    function sortFunc(a, b) {
        return sortOrder.indexOf(a[sortColumn]) - sortOrder.indexOf(b[sortColumn]);
    }
    var temp = [];
    for each(row in array.rows) temp.push(row);
    var nItems = array.rows.length;
    for (var i=0;i<nItems;i++) delete array.rows[0];
    temp.sort(sortFunc);
    for (var i=0;i<temp.length;i++) array.AddRow(temp[i]);
}
sort(result,['f','t','h','d'],'column1');

View solution in original post

4 REPLIES 4

Not super efficient, but should work:

 

function sort(array,sortOrder,sortColumn) {
    function sortFunc(a, b) {
        return sortOrder.indexOf(a[sortColumn]) - sortOrder.indexOf(b[sortColumn]);
    }
    var temp = [];
    for each(row in array.rows) temp.push(row);
    var nItems = array.rows.length;
    for (var i=0;i<nItems;i++) delete array.rows[0];
    temp.sort(sortFunc);
    for (var i=0;i<temp.length;i++) array.AddRow(temp[i]);
}
sort(result,['f','t','h','d'],'column1');

Thanks Carles it worked :)

Hi @CarlesColl 

I have one more additional requirement for info table sorting.

My sort array may not have all the values in the specified column. Only specified values of column must be sorted at beginning and remaining as per existing order.

 

Example:

Example:

My Infotable result set:

Column1 Column2 Column3
d desc1 desc one
h desc2 desc two
f desc3 desc three
t desc4 desc four

 

My order to sort  ::  [f,t]

 

Expected result set after sorting

Column1 Column2 Column3
f desc3 desc three
t desc4 desc four
d desc1 desc one
h desc2 desc two

 

Thanks in advance.

You have the clues on my previous post, just analyse it and adapt to your use case ;)

Announcements


Top Tags