public IActionResult GetPage(int page, [FromBody] SortingCondition condition) { try { var result = _service.GetEmployeesAsSortedArray(page, condition ?? SortingCondition.Default); return(Ok(result)); } catch (ArgumentException) { return(NoContent()); } }
static void Main(string[] args) { var sortingConditions = new SortingCondition[] { SortingCondition.Unassorted, SortingCondition.Reversed }; var sortingAlgorithms = new Func<IList<int>, IList<int>>[] { SortingAlgorithms.CSharpSort, SortingAlgorithms.CountingSort, SortingAlgorithms.MergeSort, SortingAlgorithms.MergeSortOptimized, SortingAlgorithms.ShellSort, SortingAlgorithms.CombSort }; SortingAlgorithmsPrinter.SortingAlgorithmsComparison(sortingAlgorithms, sortingConditions, false, 100000, -1000, 1000); /* var searchingAlgorithms = new Func<IList<int>, int, int?>[] { SearchAlgorithms.BinarySearch }; SearchingAlgorithmsPrinter.SearchingAlgorithmsComparison(searchingAlgorithms, false, 10000000, -1000, 1000); */ Console.Read(); }
/// <summary> /// Get updated filename (replace regex matches in _sortingEntry.Substitutions) /// </summary> /// <param name="fileName"></param> /// <returns></returns> private string UpdateFileName(string fileName, SortingCondition condition) { var newName = fileName; foreach (var substitution in _sortingEntry.Substitutions) { newName = Regex.Replace(newName, substitution.RegexCondition, substitution.Replacement); } foreach (var substitution in condition.Substitutions) { newName = Regex.Replace(newName, substitution.RegexCondition, substitution.Replacement); } return(newName); }
/// <summary> /// True = file content matches regex (or no condition defined) /// </summary> /// <param name="fullPath"></param> /// <returns></returns> private bool FileContentMatches(string fullPath, SortingCondition condition) { if (!string.IsNullOrWhiteSpace(condition.FileContentCondition)) { //Read content string text = ""; if (Path.GetExtension(fullPath) == ".pdf") { text = TextReader.ReadText(fullPath, FileTypes.pdf); } return(Regex.IsMatch(text, condition.FileContentCondition)); } else { return(true); } }
/// <summary> /// Move file to new destination (configure in _sortingEntry) /// </summary> /// <param name="fullPath"></param> private void MoveFile(string fullPath, SortingCondition condition) { //Get new file name var fileName = Path.GetFileName(fullPath); var newFilename = UpdateFileName(fileName, condition); //Move file to new position var fullDestinationPath = FillCustomParameters(FillDateParameters(Path.Combine(condition.DestinationFolder, newFilename))); var destinationFolder = Path.GetDirectoryName(fullDestinationPath); //Create destination folder if not exist if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } if (fullPath != fullDestinationPath) { Logger.CreateLog("Moving '" + fullPath + "' to '" + fullDestinationPath + "'"); try { while (File.Exists(fullDestinationPath)) { fullDestinationPath = IncrementFileNumber(fullDestinationPath); } File.Move(fullPath, fullDestinationPath); } catch (Exception ex) { Logger.CreateLog(ex.Message); } } }
public Employee[] GetEmployeesAsSortedArray(int page, SortingCondition condition) { IOrderedQueryable <Employee> resultSet = null; if (condition.Direction == SortingDirection.Asc) { switch (condition.Field) { case SortingField.Id: resultSet = _context.Employees.OrderBy(x => x.Id); break; case SortingField.Name: resultSet = _context.Employees.OrderBy(x => x.Name); break; case SortingField.Position: resultSet = _context.Employees.OrderBy(x => x.Position); break; case SortingField.Salary: resultSet = _context.Employees.OrderBy(x => x.Salary); break; case SortingField.EmploymentDate: resultSet = _context.Employees.OrderBy(x => x.EmploymentDate); break; case SortingField.WorkplaceNo: resultSet = _context.Employees.OrderBy(x => x.WorkplaceNo); break; } } else { switch (condition.Field) { case SortingField.Id: resultSet = _context.Employees.OrderByDescending(x => x.Id); break; case SortingField.Name: resultSet = _context.Employees.OrderByDescending(x => x.Name); break; case SortingField.Position: resultSet = _context.Employees.OrderByDescending(x => x.Position); break; case SortingField.Salary: resultSet = _context.Employees.OrderByDescending(x => x.Salary); break; case SortingField.EmploymentDate: resultSet = _context.Employees.OrderByDescending(x => x.EmploymentDate); break; case SortingField.WorkplaceNo: resultSet = _context.Employees.OrderByDescending(x => x.WorkplaceNo); break; } } var result = resultSet.Skip(PageLength * page).Take(PageLength).ToArray(); if (!result.Any()) { throw new ArgumentException(); } return(result); }
public static List<int> GetIntList(int count, int min, int max, SortingCondition sortingCondition = SortingCondition.Unassorted) { var list = new List<int>(); var random = new Random(); switch (sortingCondition) { case SortingCondition.Unassorted: { for (int i = 0; i < count; i++) list.Add(random.Next(min, max)); break; } case SortingCondition.PoorlySorted: { int sortedCount = (int)(count * 0.4); for (int i = 0; i < sortedCount; i++) list.Add(random.Next(min, max)); list.Sort(); for (int i = 0; i < count - sortedCount; i++) list.Insert(random.Next(0, list.Count), random.Next(min, max)); break; } case SortingCondition.SubstantiallySorted: { int sortedCount = (int)(count * 0.8); for (int i = 0; i < sortedCount; i++) list.Add(random.Next(min, max)); list.Sort(); for (int i = 0; i < count - sortedCount; i++) list.Insert(random.Next(0, list.Count), random.Next(min, max)); break; } case SortingCondition.Sorted: { for (int i = 0; i < count; i++) list.Add(random.Next(min, max)); list.Sort(); break; } case SortingCondition.PartiallySorted: { int firstListCount = count / 3; var firstList = new List<int>(); for (int i = 0; i < firstListCount; i++) firstList.Add(random.Next(min, max)); firstList.Sort(); int secondListCount = count / 3; var secondList = new List<int>(); for (int i = 0; i < secondListCount; i++) secondList.Add(random.Next(min, max)); secondList.Sort(); int thirdListCount = count - firstListCount - secondListCount; var thirdList = new List<int>(); for (int i = 0; i < thirdListCount; i++) thirdList.Add(random.Next(min, max)); thirdList.Sort(); list.AddRange(firstList); list.AddRange(secondList); list.AddRange(thirdList); break; } case SortingCondition.Reversed: { for (int i = 0; i < count; i++) list.Add(random.Next(min, max)); list.Sort(); list.Reverse(); break; } } return list; }