Reputation: 33615
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
Upvotes: 1
Views: 1401
Reputation: 9616
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.
Upvotes: 5
Reputation: 9615
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#">
Upvotes: 3
Reputation: 9616
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.
Upvotes: 2