0

Here is a data dump of what I am trying to sort

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

I want to sort by row first, then col. Lots of examples how to sort by one data element, but none that talk about secondary elements.

4

2 回答 2

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对象。

于 2013-11-05T21:57:13.843 回答
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#">
于 2013-11-08T12:38:16.477 回答