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

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.

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);
}
sort(result,['f','t','h','d'],'column1');```
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);
}
sort(result,['f','t','h','d'],'column1');```
6-Contributor
(To:CarlesColl)

Thanks Carles it worked :)

6-Contributor
(To: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