Forum Navigation
Tango analogue of c++ std::sort
Posted: 11/12/08 12:05:56This is what I want:
#include <iostream> #include <algorithm> #include <vector> class A { int data_; public: A( int data ) { data_ = data; } bool operator()( int a, int b ) { return abs( a - data_ ) < abs( b - data_ ); } }; int main() { const int right = 10; std::vector< int > v; for( int i=0; i<right; i++ ) v.push_back( i ); std::sort( v.begin(), v.end(), A( right/2 ) ); for( int i=0; i<v.size(); i++ ) { std::cout << i << " " << v[i] << std::endl; } }I've tried to do it for Tango && D 1.0, but can't:
import tango.io.Stdout; import tango.util.collection.ArraySeq; class Sorter { ArraySeq!( int ) array_; public: this() { array_ = new ArraySeq!( int ); } ~this() { delete array_; } int delegate( int, int ) comparator( int data ) { int compare( int a, int b ) { Stdout( data ); // trash in data int ia = tango.math.Math.abs( a-data ); int ib = tango.math.Math.abs( b-data ); if( ia < ib ) return -1; else if( ia != ib ) return 1; else return 0; } return &compare; } void work() { const int right = 10; for( int i=0; i<right; i++ ) array_ ~= i; array_.sort( comparator ( right/2 ) ); for( int i=0; i<right; i++ ) Stdout.formatln( "{} {}", i, array_[i] ); } }; int main() { auto sorter = new Sorter; sorter.work(); delete sorter; }What is the best way to do it?