本帖最后由 foboy 于 2011-4-13 19:51 编辑
献上俺写的一个函数:;排序一个数组。
;参数:$_the_array:原数组;$_mode:0-从大到小/1-从小到大;$_mode_ex:0-返回的数组内容为排序后的数值 1-返回的数组为原数组排序后的序列(角标),例如原数组是[2,6,3],按由大到小排列是[6,3,2],在此模式下函数返回的是其顺序号:[1,2,0]
Func _paixu($_the_array, $_mode = 0, $_mode_ex = 0)
$_date_num = UBound($_the_array)
Local $_xulies[$_date_num]
For $_j = 0 To $_date_num - 1
$_xulies[$_j] = $_j
Next
For $_i_index = 0 To $_date_num - 2
For $_i = 0 To $_date_num - $_i_index - 2
If $_mode == 0 Then
If $_the_array[$_i] <= $_the_array[$_i + 1] Then
$_num_linshi = $_xulies[$_i]
$_xulies[$_i] = $_xulies[$_i + 1]
$_xulies[$_i + 1] = $_num_linshi
$_num_linshi = $_the_array[$_i]
$_the_array[$_i] = $_the_array[$_i + 1]
$_the_array[$_i + 1] = $_num_linshi
EndIf
Else
If $_the_array[$_i] >= $_the_array[$_i + 1] Then
$_num_linshi = $_xulies[$_i]
$_xulies[$_i] = $_xulies[$_i + 1]
$_xulies[$_i + 1] = $_num_linshi
$_num_linshi = $_the_array[$_i]
$_the_array[$_i] = $_the_array[$_i + 1]
$_the_array[$_i + 1] = $_num_linshi
EndIf
EndIf
Next
Next
If $_mode_ex == 1 Then
Return $_xulies
Else
Return $_the_array
EndIf
EndFunc ;==>_paixu
|