|
原帖由 lb_bn 于 2010-7-29 11:46 发表
Sub bbb()
Dim x&, y&, str1$, ar1
ar1 = [a2:d5] ' [a1].Resize([a65536].End(3).Row, 3).Value
x = UBound(ar1) - 1
y = UBound(ar1, 2) - 1
Set js = CreateObject("script ...
呵呵,看样子JavaScript的sort不能直接得到二维数组了...
你的解题想法和我想的一块去了,用循环得到排序后的数组。- Sub bbb()
- Dim x&, y&, str1$, ar1, brr, crr, i%, j%
-
- ar1 = [a2:d5] ' [a1].Resize([a65536].End(3).Row, 3).Value
-
- x = UBound(ar1) - 1
- y = UBound(ar1, 2) - 1
- Set js = CreateObject("scriptcontrol")
- js.Language = "javascript"
- '按数字排序
- 'str1 = "function aa(bb){var cc=new Array();aa=bb.toArray();for(i=0;i<=" & x & ";i++){cc[i]=new Array();for(j=0;j<=" & y & ";j++){cc[i][j]=aa[" & x + 1 & "*j+i];}}return cc.sort(function(x, y){return (x[1]==y[1])?(x[0]-y[0]):(x[1]-y[1])});}"
- '按字符排序
- str1 = "function aa(bb){var cc=new Array();aa=bb.toArray();for(i=0;i<=" & x & ";i++){cc[i]=new Array();for(j=0;j<=" & y & ";j++){cc[i][j]=aa[" & x + 1 & "*j+i];}}return cc.sort(function(x, y){a=new String(x[0]);b=new String(x[1]);return (x[1]==y[1])?(a.localeCompare(y[0])):(b.localeCompare(y[1]))});}"
- js.AddCode str1
- brr = Split(js.codeobject.aa(ar1), ",")
-
- ReDim crr(1 To UBound(ar1), 1 To UBound(ar1, 2))
-
- For i = 0 To UBound(brr) Step 4
- For j = 1 To 4
- crr(i / 4 + 1, j) = brr(i + j - 1)
- Next
- Next
- [g2].Resize(UBound(crr), UBound(crr, 2)) = crr
- End Sub
复制代码 另外再请教一下,如果以第4列为关键字排序,str1 该怎么该,JavaScript的语法有点看不懂,不知道该怎么改。
非常感谢
[ 本帖最后由 jackyun 于 2010-7-29 12:40 编辑 ] |
|