如何使用字节顺序按字典顺序对结构重新排序?

发布于 2024-12-04 10:31:31 字数 199 浏览 0 评论 0原文

使用 API 进行开发,我在 ColdFusion 中有一个结构。我需要使用字节顺序按字典顺序对结构重新排序。

“按名称字典顺序对参数进行排序 [原文如此](字节排序,标准排序,不自然或不区分大小写)。如果参数具有相同的名称,则按值排序。”

采用 ColdFusion 9 中的结构,如何重新排序以符合上述要求? JAVA 库?

谢谢

Developing with an API, I have a structure in ColdFusion. I need to re-sort the structure using byte ordering lexicographically.

"Sort the parameters by name lexicographically [sic] (byte ordering, the standard sorting, not natural or case insensitive). If the parameters have the same name, then sort by the value."

Taking a structure in ColdFusion 9, how can I reorder it to comply with the above? JAVA Lib?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁杀 2024-12-11 10:31:31

我将把这个作为一个单独的答案发布,因为我相信我的第一个答案是不正确的......让我们尝试这个:

<cfset myStruct = structNew() />
<cfset mystruct["Apple"] = 1 />
<cfset mystruct["Banana"] = 2 />
<cfset mystruct["car"] = 5 />
<cfset mystruct["Tomato"] = 3 />
<cfset mystruct["aardvark"] = 4 />

<cfset Keys = StructKeyArray(myStruct) />
<cfset ArraySort(Keys, "textnocase") />

<cfdump var="#Keys#">

这会给你一个按字典顺序排序的键数组,忽略所有大小写。 StructSort 函数根据键值而不是键名称进行排序。

I'm going to post this as a separate answer, because I believe my first one is incorrect... Let's try this one instead:

<cfset myStruct = structNew() />
<cfset mystruct["Apple"] = 1 />
<cfset mystruct["Banana"] = 2 />
<cfset mystruct["car"] = 5 />
<cfset mystruct["Tomato"] = 3 />
<cfset mystruct["aardvark"] = 4 />

<cfset Keys = StructKeyArray(myStruct) />
<cfset ArraySort(Keys, "textnocase") />

<cfdump var="#Keys#">

That will give you an array of keys sorted lexicographically, ignoring all casing. The StructSort function was sorting on the key values, not the key names.

琉璃梦幻 2024-12-11 10:31:31

抱歉我的困惑,但是字典排序和自然排序(至少对于 Java 字符串)不是同一件事吗?如果是这样,请查看 Java TreeMap,看看它是否按您希望的方式工作。

<cfset myStruct = structNew() />
<cfset mystruct["Apple"] = 1 />
<cfset mystruct["Banana"] = 2 />
<cfset mystruct["car"] = 5 />
<cfset mystruct["Tomato"] = 3 />
<cfset mystruct["aardvark"] = 4 />


<cfset myMap = createObject("java","java.util.TreeMap").init(myStruct) />

<cfdump var="#myMap#">

Sorry for my confusion, but aren't lexicographic sorting and natural sorting (at least with Java Strings) the same thing? If so, take a look at the Java TreeMap and see if this works the way you want it to.

<cfset myStruct = structNew() />
<cfset mystruct["Apple"] = 1 />
<cfset mystruct["Banana"] = 2 />
<cfset mystruct["car"] = 5 />
<cfset mystruct["Tomato"] = 3 />
<cfset mystruct["aardvark"] = 4 />


<cfset myMap = createObject("java","java.util.TreeMap").init(myStruct) />

<cfdump var="#myMap#">
泡沫很甜 2024-12-11 10:31:31

您无法从技术上对结构进行排序,并保证该结构将保持其顺序。 ColdFusion 有一个坏习惯,即在添加/删除节点时任意(至少据我所知)重新排序结构。

但是,您可以获取已排序键的列表,然后可以使用它来循环遍历您的结构。使用 StructSort 方法获取数组已排序的键名。您可以将排序顺序指定为 textnocase 来获取字典顺序(忽略所有大小写)。

然后,您需要进行一些额外的排序,以便在此之后按值获取它们...如果您想要更多详细信息,最好发布一些您已经尝试过但不太有效的代码。

You can't technically sort a structure, and guarantee that the structure will maintain it's order. ColdFusion has a bad habit of arbitrarily (at least as far as I can tell) reordering structures when you add/remove nodes.

You can, however, get a list of sorted keys, which you can then use to loop through your struct. Use the StructSort method to get an array of sorted key names. You can specify the sort order as textnocase to get your lexicographic order (disregards all casing).

You would then need to do some additional sorting to get them by value after that... If you want more detail, you'll be better off posting some code you've already tried that's not quite working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文