MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ create_multi_key_comparator()

template<typename T >
auto MayaFlux::Yantra::create_multi_key_comparator ( const std::vector< SortKey > &  keys)

Creates a multi-key comparator for complex sorting.

Template Parameters
TData type being sorted
Parameters
keysVector of sort keys with extractors and weights
Returns
Lambda comparator that applies all keys in order

Definition at line 358 of file SortingHelper.hpp.

359{
360 return [keys](const T& a, const T& b) -> bool {
361 for (const auto& key : keys) {
362 try {
363 double val_a = key.extractor(std::any(a));
364 double val_b = key.extractor(std::any(b));
365
366 if (key.normalize) {
367 val_a = std::tanh(val_a);
368 val_b = std::tanh(val_b);
369 }
370
371 double weighted_diff = key.weight * (val_a - val_b);
372 if (std::abs(weighted_diff) > 1e-9) {
373 return key.direction == SortingDirection::ASCENDING ? weighted_diff < 0 : weighted_diff > 0;
374 }
375 } catch (...) {
376 continue;
377 }
378 }
379 return false;
380 };
381}

References ASCENDING.