查询 coldfusion 中的行返回排序结构

query to row in coldfusion returning sorted structure

我有以下函数,但该函数返回给我一个从 a 到 z 的可排序格式的结构

<cffunction name="rowToStruct" access="public" returntype="struct" output="false">
    <cfargument name="queryObj" type="query" required="true" />
    <cfargument name="row" type="numeric" required="true" />
    <cfset var returnStruct = structNew()>
    <cfset var colname = "">
    <cfset cList = arrayToList(arguments.queryObj.getMetaData().getColumnLabels())>
    <cfdump var="#cList#">
    <cfloop list="#cList#" index="colname">
      <cfset "returnStruct.#colname#" = arguments.queryObj[colname][arguments.row]>  
    </cfloop>
    <cfreturn returnStruct/>
</cffunction>

上面的代码是这样的

BE       Ice
BU       Net
CView     0
CMarketing  0
CProducts   1
CTraining    1
CEProducts   1
Pion     [empty string]
Status   Active
Title    [empty string]
UserType    Mode
Wizard

需要这样

SELECT TOP 1
        [UserType],
        [Pion],
        [Title],
        [Be],
        [BU],
        [Status],
        [CView],
        [CTraining],
        [CMarketing],
        [CEProducts],
        [CProducts],
        [Wizard]

CF 截至目前 (v11) 没有 LinkedHashMap,但它非常容易使用。只需使用此 orderedStructNew() 函数即可。

/** return <code>java.util.LinkedHashMap</code> that preserves order of insertion */
struct function orderedStructNew()
{
    return createObject("java","java.util.LinkedHashMap").init();
}

然后

<cfset returnStruct = orderedStructNew()>
<cfset columnLabels = queryObj.getMetaData().getColumnLabels()>
<cfloop array="#columnLabels#" index="local.colname">
  <cfset returnStruct[colname] = queryObj[colname][arguments.row]>
</cfloop>

要验证,请使用 <cfloop collection="#returnStruct#" item="local.key"> 并查看插入顺序中的键,而不是依赖 <cfdump>