You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6. /// <summary>
  7. /// Utility Class used for common methodes which don't fit in a class
  8. /// </summary>
  9. public static class Utility
  10. {
  11. /// <summary>
  12. /// Gets the smallest item in an array based on the provided function
  13. /// </summary>
  14. /// <typeparam name="T">Array type</typeparam>
  15. /// <param name="values">Array to find smallest item in</param>
  16. /// <param name="predicate">What to compare items with</param>
  17. /// <returns></returns>
  18. public static T minBy<T>(T[] values, Func<T,IComparable> predicate)
  19. {
  20. //if empty return null
  21. if (values.Length == 0)
  22. return default;
  23. //Define smallest value as first
  24. T min = values[0];
  25. //compare all other values
  26. for (int i = 1; i < values.Length; i++)
  27. if (predicate(values[i]).CompareTo(predicate(min)) < 0)
  28. min = values[i];
  29. //return
  30. return min;
  31. }
  32. }