Find an array of helper class unity

Find an array of helper class unity

Use generics, delegates, arrays, and to sort, search and other methods, the versatility of the code is very high, there are deficiencies also want bigwigs give suggestions
// here mainly for understanding the meaning of trust and arguments
// delegate is a data type
// first parameter is an array, a second parameter is the expression Lang Muda
public selectHandler the delegate TKey <T, TKey> (T T);
public BOOL FindHandler the delegate (T T);
public static class ArrayHelper
{
///
/// ascending
///
/// data type
/// data type attribute
array data type object ///
/// delegate object for selecting a field from a certain type and returns the value of the field
///
static public void the OrderBy <T, TKey>
(T [] Array, selectHandler <T, TKey> Handler)
WHERE TKey: the IComparable
{
for (int I = 0; I <array.length; ++ I)
{
for (int I = J +. 1; J <array.length; J ++)
{
// handler (array [j]) returns a delegate call attributes need to be compared
IF (. Handler (Array [I]) the CompareTo (Handler (Array [J]))> 0)
{
var Array TEMP = [I];
Array [I] = Array [J];
Array [J] = TEMP;
}
}
}
}
///
/// descending order
///
/// data type
type field's data type ///
/// data type an array of objects
///
/// delegate object: selecting a field from a charge of the type of return value of this field
///
static public void OrderByDescending <T, TKey>
(T [] array, selectHandler <T, TKey > Handler)
WHERE TKey: the IComparable
{
for (int I = 0; I <array.length; I ++)
{
for (int I = J +. 1; J <array.length; J ++)
{
IF (Handler (Array [I]) the CompareTo (Handler (Array [J])) <0.)
{
var TEMP = Array [I];
Array [I] = Array [J];
Array [J] = TEMP;
}
}
}
}
///
/// returns the largest
///
/// data type
/// type field's data type
array data type of the object ///
///
/// delegate candidate: from a certain type select a field return value of this field
///
static public Max T <T, TKey>
(T [] Array, selectHandler <T, TKey> Handler)
WHERE TKey: the IComparable
{
T default TEMP = (T); / / initialized
TEMP = Array [0];
for (int I =. 1; I <array.length; I ++)
{
IF (Handler (TEMP) .CompareTo (Handler (Array [I])) <0)
{
TEMP = Array [i];
}
}
Return TEMP;
}
///
/// Returns the minimum
///
/// Data Type
Type of Data Type field ///
/// array data type objects
///
/// delegate candidate: From a type field in a selected return value of this field
///
static public Min T <T, TKey>
(T [] Array, selectHandler <T, TKey> Handler)
WHERE TKey: the IComparable
{
T default TEMP = (T );
TEMP = Array [0];
for (int I =. 1; I <array.length; I ++)
{
IF (Handler (TEMP) .CompareTo (Handler (Array [I]))> 0)
{
TEMP = Array [ I];
}
}
return TEMP;
}
// array of tools general method lookup implementation the Find
static public the Find T (T [] array, FindHandler Handler)
{
T default TEMP = (T);
for (int I = 0; I <array.length; I ++)
{
IF (Handler (Array [I]))
{
return Array [I];
}
}
return TEMP;
}
// Find all the ways the FindAll
// given a look condition? Returns all satisfy the conditions of
static public T [] the FindAll (T [] Array, FindHandler Handler)
{
List List List new new = ();
for (int I = 0; I <array.length; I ++)
{
IF (Handler ( array [I]))
{
list.add (array [I]);
}
}
return List.toArray ();
}
// selection: select some members in the array of objects to form a separate array of
static public TKey [] select <T, TKey> (T [] Array, selectHandler <T, TKey> Handler)
{
TKey[] keys = new TKey[array.Length];
for (int i = 0; i < array.Length; i++)
{
keys[i] = handler(array[i]);
}
return keys;
}
}

Guess you like

Origin blog.csdn.net/weixin_43333566/article/details/94762951
Recommended