按客户订单ColdFusion的数组排序 [英] sorting array by custom order in Coldfusion
问题描述
Ive得到其可以变化的产品尺寸的阵列
Ive got an array of product sizes which can be varied.
某些情况下,这将是。
08
10
12
16
18
S
M
L
XL
XXL
我似乎能够在大小有串它也数值数组但是容易排序,我似乎无法给他们按字母进行排序,以及
I appear to be able to sort the array of numeric values easily, however when the sizing has strings with it also, i cant seem to sort them in alphabetical order as well
例
它最终被
L
M
S
XL
XXL
technicailly讲,这是用于字母顺序正确的,但是对人类为它的顺序应
technicailly speaking, this is correct for alphabetical order, but the order for it for human beings should be
S
M
L
XL
XXL
会有人知道的方式做到这一点使用ColdFusion?即时通讯目前使用CF 8,我到处找排序,但似乎无法找出如何做到这一点。
Would anyone know of a way to do this using Coldfusion ? Im currently using CF 8 and have looked everywhere for sorting, but cant seem to find out how to do it
推荐答案
如果你使用的CF10 +,你很幸运!
If you're using CF10+, you're in luck!
https://wikidocs.adobe.com/wiki/display/coldfusionen/ArraySort
实现自己的回调函数来排序您的收藏自己,例如:
Implement your own callback function to sort your collection yourself, e.g.:
<cfscript>
function sortSize(array sizes)
{
var sizeOrder = "XXS,XS,S,M,L,XL,XXL"; // your sort order
arraySort(sizes, function(s1, s2) {
return listFindNoCase(sizeOrder,s1) - listFindNoCase(sizeOrder,s2);
});
return sizes;
}
</cfscript>
HTTP: 在TryCF运行code以上// WWW。 trycf.com/scratch-pad/pastebin?id=pb1rn6CT
更新:但既然你用CF8,你可以实现自己的冒泡排序,并尝试:
Update: But since you're using CF8, you can implement your own bubble sort and try:
<cfscript>
function sortSize(input)
{
var local = {};
local.sizes = "XXS,XS,S,M,L,XL,XXL"; // your sort order
local.sorted = false;
local.inputSize = arrayLen(input);
while (!local.sorted)
{
local.sorted = true;
for (local.i = 1; local.i < local.inputSize; local.i = local.i + 1)
{
local.size1 = listFindNoCase(local.sizes, input[local.i]);
local.size2 = listFindNoCase(local.sizes, input[local.i + 1]);
if (local.size1 > local.size2)
{
arraySwap(input, local.i, local.i + 1);
local.sorted = false;
}
}
}
return input;
}
</cfscript>
<cfdump var="#sortSize(listToArray('l,M,M,s,XL,Xs'))#">
HTTP: 在TryCF运行code以上// WWW。 trycf.com/scratch-pad/pastebin?id=uL7r9Uyf
这篇关于按客户订单ColdFusion的数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!