这是我想要排序的数据转储
array
1
struct
col 1
dataid 48
identifier 1
row 1
size_x 4
size_y 1
2
struct
col 1
dataid 42
identifier 2
row 2
size_x 2
size_y 1
3
struct
col 3
dataid 45
identifier 3
row 2
size_x 2
size_y 1
我想先按row
排序,然后col
排序。大量示例如何按一个数据元素排序,但没有一个讨论次要元素。
答案 0 :(得分:2)
ColdFusion 10使用回调内置自定义数组排序。 arraySort()
的文档没有提到这一点,但我刚刚用一个例子更新了它们。我的例子没有显示你需要的复合排序,但它很容易:
<cfscript>
comparator = function(e1, e2){
e1.row += 0; // need to make sure it's not a string for the Java method call below
var rowCompare = e1.row.compareTo(e2.row + 0);
if (rowCompare !=0){
return rowCompare;
}
e1.col += 0;
return e1.col.compareTo(e2.col + 0);
};
data = [
{row=3, col=3}, {row=3,col=2}, {row=3, col=1},
{row=2, col=3}, {row=2,col=2}, {row=2, col=1},
{row=1, col=3}, {row=1,col=2}, {row=1, col=1}
];
writeDump(var=data);
arraySort(data, comparator);
writeDump(var=data);
</cfscript>
这可以利用CF数字是java.lang.Double
个对象。
答案 1 :(得分:0)
<cfscript>
//ColdFusion 10 only supports this new types of struct declaration
recordArr = [
{col: 1,dataid:48,identifier:1,row:1,size_x:4,size_y:1},
{col: 1,dataid:42,identifier:2,row:2,size_x:2,size_y:1},
{col: 3,dataid:45,identifier:3,row:2,size_x:2,size_y:1}
];
//ColdFusion 10 only supports this new queryNew() functionality
queryObj = queryNew("col,dataid,identifier,row,size_x,size_y",
"Integer,Integer,Integer,Integer,Integer,Integer",
recordArr);
</cfscript>
<!--- Here it comes our favourite cfquery tag. We can apply order by clause
as per our wish --->
<cfquery name="ordredResult" dbtype="query">
SELECT * FROM queryObj ORDER BY row ASC, col ASC
</cfquery>
<!--- Here is the expected result --->
<cfdump var="#ordredResult#">