cancel
Showing results for 
Search instead for 
Did you mean: 
Security Alert Log4j Security Vulnerability. Click here to know more.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting an infotable based on order specified

polasaikrishna
5-Regular Member

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.

1 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');
polasaikrishna
5-Regular Member
(To:CarlesColl)

Thanks Carles it worked :)

polasaikrishna
5-Regular Member
(To:CarlesColl)

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