System::Comparison< T > Class Template Reference

Inherits MulticastDelegate< int(T, T)>.

Public Member Functions

template<typename Y >
 Comparison (Y arg)
bool operator() (T a, T b)

Detailed Description

template<typename T>
class System::Comparison< T >

Represents a pointer to the method that compares two objects of the same type. This type should be allocated on stack and passed to functions by value or by reference. Never use System::SmartPtr class to manage objects of this type.

#include "system/comparison.h"
#include "system/console.h"
#include "system/exceptions.h"
#include "system/string.h"
#include <algorithm>
#include <initializer_list>
#include <vector>
using namespace System;
// The template class that represents a dynamic array.
template <typename T>
class MyArray
// Used to store the array data.
std::vector<T> m_data;
// Constructs a new instance of our dynamic array.
MyArray(const std::initializer_list<T>& source) : m_data(source) {};
// Used to sort the array data. This method accepts an instance of the
// 'System::Comparison' template class.
void Sort(Comparison<T> comparison)
if (comparison.IsNull())
throw ArgumentNullException(u"comparison");
std::sort(m_data.begin(), m_data.end(), comparison);
// Returns a number of elements that our dynamic array stores.
size_t get_Size()
return m_data.size();
// Used to get an element at the specified index.
T& operator[](int index)
if (index < 0 || index >= m_data.size())
throw IndexOutOfRangeException(u"index");
return m_data[index];
int main() {
// Create an instance of the MyArray class with the specified elements.
MyArray<String> arr = {u"a", u"e", u"c", u"b", u"d"};
// Sort by ascending elements of the dynamic array.
arr.Sort([](const String &a, const String &b) -> int
return String::Compare(a, b);
// Print elements of the dynamic array.
for (auto i = 0; i < arr.get_Size(); ++i)
return 0;
* This code example produces the following output:
* a
* b
* c
* d
* e
Template Parameters
TThe type of the objects the method compares

Constructor & Destructor Documentation

◆ Comparison()

template<typename T>
template<typename Y >
System::Comparison< T >::Comparison ( arg)

Constructs an instance of Comparison delegate that represent the pointer to the specified invokable entity.

Template Parameters
YThe type of the invokable entity to be pointed to by the Comparison object being created
argAn invokable entity to be pointed to by the Comparison object being created

Member Function Documentation

◆ operator()()

template<typename T>
bool System::Comparison< T >::operator() ( a,

Invokes the invokable object pointed to by the current object.

aThe first comparand
bThe second comparand
True if a is less than b, otherwise - true