排序ColdFusion的二维数组 [英] Sorting a 2D array in Coldfusion

查看:120
本文介绍了排序ColdFusion的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想根据2值数组排序。第一个工作正常,但我不能似乎得到第二个权

I have an array that I would like to sort according to 2 values. The first one works fine but i cant seem to get the second one right

这是我如何根据第一个值排序:

This is how I am sorting according to the first value:

<cfloop index="outer" from="1" to="#arrayLen(result)#">
    <cfloop index="inner" from="1" to="#arrayLen(result)-1#">
        <cfif result[inner][7] lt result[outer][7]>
            <cfset arraySwap(result,inner,outer)>
        </cfif>
    </cfloop>
</cfloop>

这是我想排序的第二个值不是一个数值,它是一个人的姓氏。它是在阵列中的第2个项目

The second value that I would like to sort by is not a numeric value, it is a persons surname. It is the 2nd item in the array

我怎么可能去通过阵列中的这两个值进行排序,都必须降。我也不想用的人的长短进行排序姓,但按字母顺序排序它们

How could I go about to sort by both these values in the array, both have to be descending. I also do not want to sort by the length of the persons surname but sort them alphabetically

推荐答案

正如亚当卡梅伦指出,如果有大型数据集嵌套的循环可以得到极其沉重。我会离开我的前code到位使用旧的CFs的人。

As Adam Cameron points out, nested looping can get extremely heavy if you have large datasets. I'll leave my former code in place for people using older CFs.

不过,如果你有CF10 / Railo 4或以上,以下code应该工作更好地为您。它产生完全相同的结果,同时速度更快。

However, if you have CF10/Railo 4 or above, the following code should work better for you. It produces exactly the same results while being faster.

如下,这仍然使用一个基本的样机阵列和第二个元素相较于OP的第七(在他的更复杂的数据集)。

As below, this still uses a basic mockup array and the second element vs the OP's seventh (in his more complex dataset).

<cfset Result = [["dan","smith"],["dan","Johnson"],["bob","jones"],["dan","smith"],["dan","johnson"],["bob","jones"],["cfquery","param"],["steve","buscemi"],["dan","smith"],["dan","johnson"],["bob","jones"]]>

<Cfset arraySort(result, function (current_element, next_element) {
    return comparenocase(current_element[2], next_element[2]);
  })>

<cfdump var="#result#">

您可以在此采取步骤(或多个步骤进一步)被太多比较的第一个名字,或任何其他数据。

You can take this a step (or many steps further) by comparing the first name too, or any other data.

<cfset Result = [["dan2","smith"],["dan1","Johnson"],["bob","jones"],["dan3","smith"],["dan0","johnson"],["bob","jones"],["cfquery","param"],["steve","buscemi"],["dan","smith"],["dan","johnson"],["bob","jones"]]>

<Cfset arraySort(result, function (current_element, next_element) {
    return comparenocase("#current_element[2]#, #current_element[1]#", "#next_element[2]#, #next_element[1]#");
  })>

<cfdump var="#result#">

这篇关于排序ColdFusion的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆