C# 反射算法 数组排序

#region 反射算法来自于discuz
/// <summary>
/// 反射出指定对象实例的所有属性值
/// </summary>
/// <param name="obj">指定对象实例</param>
/// <returns></returns>
public static string[] GetUrlParam(object obj) {
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

string urlParam = "";
foreach (PropertyInfo pi in propertyInfos) {
if (pi.GetValue(obj, null) != null) {
if (pi.Name == "Sign" || pi.Name == "Sign_Type") {
continue;
}

//物流信息时
if (pi.Name == "Logistics") {
Logistics[] logisticsInfoArray = ((NormalTrade) obj).Logistics;
int i = 0;
foreach (Logistics logisticsInfo in logisticsInfoArray) {
if (logisticsInfo.Logistics_Type != "") {
//物流参数的下标(第一种物流方式为"",第二种为"_1",第三种为"_2",以此类推)
string orderflag = "";
if (i > 0) {
orderflag = "_" + i;
}
urlParam += "logistics_type" + orderflag + "=" + logisticsInfo.Logistics_Type + "&logistics_fee" + orderflag + "=" + logisticsInfo.Logistics_Fee + "&logistics_payment" + orderflag + "=" + logisticsInfo.Logistics_Payment + "&";
i++;
}
}
} else {
urlParam += pi.Name.ToLower().Replace("input_charset", "_input_charset") + "=" + pi.GetValue(obj, null).ToString() + "&";
}
}
}

if (urlParam.EndsWith("&")) {
urlParam = urlParam.Substring(0, urlParam.Length - 1);
}

return urlParam.Split('&');
}
#endregion
#region 排序算法来自discuz
/// <summary>
/// 把数组划分为两个部分
/// </summary>
/// <param name="strArray">划分的数组</param>
/// <param name="low">数组低端上标</param>
/// <param name="high">数组高端下标</param>
/// <returns></returns>
public static int Partition(string[] strArray, int low, int high) {
//进行一趟快速排序,返回中心轴记录位置
// arr[0] = arr[low];
string pivot = strArray[low];//把中心轴置于arr[0]
while (low < high) {
while (low < high && System.String.CompareOrdinal(strArray[high], pivot) >= 0)
--high;
//将比中心轴记录小的移到低端
Swap(ref strArray[high], ref strArray[low]);
while (low < high && System.String.CompareOrdinal(strArray[low], pivot) <= 0)
++low;
Swap(ref strArray[high], ref strArray[low]);
//将比中心轴记录大的移到高端
}
strArray[low] = pivot; //中心轴移到正确位置
return low; //返回中心轴位置
}

public static void Swap(ref string i, ref string j) {
string t;
t = i;
i = j;
j = t;
}

/// <summary>
/// 快速排序算法
/// </summary>
/// <param name="strArray">划分的数组</param>
/// <param name="low">数组低端上标</param>
/// <param name="high">数组高端下标</param>
public static void QuickSort(string[] strArray, int low, int high) {
if (low <= high - 1)//当 arr[low,high]为空或只一个记录无需排序
{
int pivot = Partition(strArray, low, high);
//左子树
QuickSort(strArray, low, pivot - 1);
//右子树
QuickSort(strArray, pivot + 1, high);
}
}
#endregion



你可能感兴趣的:(C#)