xtd 1.0.0
Loading...
Searching...
No Matches
array_sort3.cpp

Shows how to use xtd::basic_array::sort method.

#include <xtd/xtd>
struct my_own_descending_comparer : icomparer<int> {
[[nodiscard]] auto compare(const int& x, const int& y) const -> int override {return x > y ? -1 : x < y ? 1 : 0;}
};
auto main() -> int {
auto items = array {10, 6, 5, 7, 8, 2, 9, 3, 1, 4};
// C++ standard
//items.sort(2, 6, [](auto&& a, auto&& b) {return a < b;});
items.sort(2, 6, _1 < _2);
println("ascending sorted items = {}", items);
//items.sort(2, 6, [](auto&& a, auto&& b) {return a > b;});
items.sort(2, 6, _1 > _2);
println("descending sorted items = {}", items);
// C++ modern with ordering
//items.sort(2, 6, [](auto&& a, auto&& b) {return a <=> b;});
items.sort(2, 6, _1 <=> _2);
println("ascending sorted items = {}", items);
//items.sort(2, 6, [](auto&& a, auto&& b) {return a <=> b == std::strong_ordering::greater;});
items.sort(2, 6, _1 <=> _2 == std::strong_ordering::greater);
println("descending sorted items = {}", items);
// C# style
//items.sort(2, 6, [](auto&& a, auto&& b) {return a > b ? 1 : a < b ? -1 : 0;});
items.sort(2, 6, if_then_else(_1 > _2, 1, if_then_else(_1 < _2, -1, 0)));
println("ascending sorted items = {}", items);
//items.sort(2, 6, [](auto&& a, auto&& b) {return a > b ? -1 : a < b ? 1 : 0;});
items.sort(2, 6, [](auto&& a, auto&& b) {return a > b ? -1 : a < b ? 1 : 0;});
println("descending sorted items = {}", items);
// C# style with comparer
items.sort(2, 6, comparer<int>::default_comparer);
println("ascending sorted items = {}", items);
items.sort(2, 6, my_own_descending_comparer {});
println("descending sorted items = {}", items);
}
// This code produces the following output :
//
// ascending sorted items = [10, 6, 2, 3, 5, 7, 8, 9, 1, 4]
// descending sorted items = [10, 6, 9, 8, 7, 5, 3, 2, 1, 4]
//
// ascending sorted items = [10, 6, 2, 3, 5, 7, 8, 9, 1, 4]
// descending sorted items = [10, 6, 9, 8, 7, 5, 3, 2, 1, 4]
//
// ascending sorted items = [10, 6, 2, 3, 5, 7, 8, 9, 1, 4]
// descending sorted items = [10, 6, 9, 8, 7, 5, 3, 2, 1, 4]
//
// ascending sorted items = [10, 6, 2, 3, 5, 7, 8, 9, 1, 4]
// descending sorted items = [10, 6, 9, 8, 7, 5, 3, 2, 1, 4]
//
constexpr auto if_then_else(condition_t condition, then_t then_, else_t else_)
If condition is true return then_ else retur nelse_.
Definition if_then_else_expression.hpp:92
@ a
The A key.
Definition console_key.hpp:88
@ b
The B key.
Definition console_key.hpp:90
auto println(FILE *file) -> void
Writes the current line terminator to the file output stream using the specified format information.
Definition println.hpp:15