use*_*065 1 coldfusion coldfusion-9 cfml
说我有以下列表:
<cfset myList = "1a,2b,3c,aa,bb,cc" >
Run Code Online (Sandbox Code Playgroud)
如何对此列表进行排序,使其成为"aa,bb,cc,1a,2b,3c"?换句话说,我希望以数字开头的任何内容都位于列表的末尾,并按照它开头的数字顺序排列.
如果您使用CF10 +,这将有效.
<cfscript>
values = listToArray("1a,2b,3c,aa,bb,cc");
arraySort(values, function(e1, e2) {
var diff = val(e1) - val(e2);
if(diff != 0)
return diff;
return e1 < e2 ? -1 : 1;
});
writedump(values);
</cfscript>
Run Code Online (Sandbox Code Playgroud)
运行:http://www.trycf.com/scratch-pad/pastebin?id = kKY9y2Nn
UPDATE
但是既然你正在使用CF9:
<cfscript>
function customSort(input) {
var sorted = false;
while (!sorted) {
sorted = true;
for (var i = 1; i < arrayLen(input); i++) {
var e1 = input[i];
var e2 = input[i + 1];
var diff = val(e1) - val(e2);
if (diff == 0 ? e1 > e2 : diff > 0) {
arraySwap(input, i, i + 1);
local.sorted = false;
}
}
}
return input;
}
values = listToArray("1a,2b,3c,3a,aa,bb,cc");
writeDump(customSort(values));
</cfscript>
Run Code Online (Sandbox Code Playgroud)
运行:http://www.trycf.com/scratch-pad/pastebin?id = XK3fQv9T
| 归档时间: |
|
| 查看次数: |
970 次 |
| 最近记录: |