|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using System;
-
- /// <summary>
- /// Utility Class used for common methodes which don't fit in a class
- /// </summary>
- public static class Utility
- {
-
- /// <summary>
- /// Gets the smallest item in an array based on the provided function
- /// </summary>
- /// <typeparam name="T">Array type</typeparam>
- /// <param name="values">Array to find smallest item in</param>
- /// <param name="predicate">What to compare items with</param>
- /// <returns></returns>
- public static T minBy<T>(T[] values, Func<T,IComparable> predicate)
- {
- //if empty return null
- if (values.Length == 0)
- return default;
-
- //Define smallest value as first
- T min = values[0];
-
- //compare all other values
- for (int i = 1; i < values.Length; i++)
- if (predicate(values[i]).CompareTo(predicate(min)) < 0)
- min = values[i];
-
- //return
- return min;
- }
- }
|