public static int[] Sort(int[] data, SortMethodEnum sortMethod) { for (int i = 0; i < data.Length; i++) { for (int j = 0; j < data.Length; j++) { if (sortMethod == SortMethodEnum.FromLargeToSmall) { if (data[i] > data[j]) { var temp = data[i]; data[i] = data[j]; data[j] = temp; } } else { if (data[i] < data[j]) { var temp = data[i]; data[i] = data[j]; data[j] = temp; } } } } return(data); }
public static int[] Sort(int[] data, SortMethodEnum sortMethod) { for (int i = 0; i < data.Length; i++) { if (sortMethod == SortMethodEnum.FromLargeToSmall) { int maxDataIndex = i; for (int j = i; j < data.Length; j++) { if (data[j] > data[maxDataIndex]) { maxDataIndex = j; } } var temp = data[maxDataIndex]; data[maxDataIndex] = data[i]; data[i] = temp; } else { int minDataIndex = i; for (int j = i; j < data.Length; j++) { if (data[j] < data[minDataIndex]) { minDataIndex = j; } } var temp = data[minDataIndex]; data[minDataIndex] = data[i]; data[i] = temp; } } return(data); }
public Settings(string basePath, string file) { IConfiguration config = new ConfigurationBuilder() .SetBasePath(basePath) .AddJsonFile(file) .Build(); NumberCount = int.TryParse(config[nameof(NumberCount)], out var numberCount) ? numberCount : defaultNumberCount; SourceFile = config[nameof(SourceFile)]; ResultFile = config[nameof(ResultFile)]; SortMethod = Enum.TryParse <SortMethodEnum>(config[nameof(SortMethod)], out var sortMethod) && Enum.IsDefined(sortMethod) ? sortMethod : defaultSortMethod; }
/// <summary> /// /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="source"></param> /// <param name="property"></param> /// <param name="methodName"></param> /// <returns></returns> public static IOrderedQueryable <TEntity> ApplyOrder <TEntity>(this IQueryable <TEntity> source, string property, SortMethodEnum methodName) { var props = property.Split('.'); var type = typeof(TEntity); var arg = Expression.Parameter(type, "x"); Expression expr = arg; foreach (var prop in props) { // use reflection (not ComponentModel) to mirror LINQ var pi = type.GetProperty(prop); expr = Expression.Property(expr, pi); type = pi.PropertyType; } var delegateType = typeof(Func <,>).MakeGenericType(typeof(TEntity), type); var lambda = Expression.Lambda(delegateType, expr, arg); var result = typeof(Queryable).GetMethods().Single( method => method.Name == methodName.GetDescription() && method.IsGenericMethodDefinition && method.GetGenericArguments().Length == 2 && method.GetParameters().Length == 2) .MakeGenericMethod(typeof(TEntity), type) .Invoke(null, new object[] { source, lambda }); return((IOrderedQueryable <TEntity>)result); }