Stop conditionally std::sort

I want to ask : exist a way to stop (interrupt) a sorting process through std::sort given a comparation function ? Inside comparation function exist heavy calculations, but also it can happen that a absolute result of comparation (<) is not possible, in which case i want to interrupt this sorting process in order to start another type of sorting process.
Only it is possible using "trow" and "catch" or exist another way ?

thanks for any help, Daniel
Couldn't you just do the comparison first before attempting std::sort?
You mean, compare every element with every element ?
My god, i told you this is expensive in termes of time.
My another idea of course, is to create a sorting algorithm or copy std::sort's code, and change it, with this extra, but my question is if or not exist a way to interrupt this process, with some kind of condition ?
True but what happens to your container if you interrupt sort mid-way?
It will give up to have a order-result, in will conform to only contain groups : which elements are same (equivalents) , and which elements are different (non equivalents). This algorithm not will give results which are so precise as another ordering by <, because we only can sort this elements (which are agrouped by != and ==) by number of elements by group, which not is in all cases satisfactory.
So, i will first try to obtain a order off this elements, and if this fails, use results af agroupments.
greatings, and thanks
> a way to stop (interrupt) a sorting process through std::sort given a comparation function ?
> Only it is possible using "trow" and "catch" or exist another way ?

There is no way to interrupt std::sort mid-way and then later resume the sort from where it was interrupted.

std::sort can be aborted by throwing an exception from the comparison predicate; however the sequence would be in some unspecified order after that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
#include <stdexcept>

struct cmp_dbl
{
    static double LessThanComparable( double v )
    {
        if( v != v ) throw std::domain_error( "NaN - not LessThanComparable" ) ;
        return v ;
    }

    bool operator() ( double a, double b ) const
    { ++cnt ; return LessThanComparable(a) < LessThanComparable(b) ; }

    static int cnt ;
};

int cmp_dbl::cnt = 0 ;

int main()
{
    double arr[] { 1.2, 3.4, 5,6, 7.8, 0.0, 9.1, 2.3, 4.5, 6.7, 8.9 } ;
    for( double& v : arr ) v /= 0.0 ;

    try { std::sort( std::begin(arr), std::end(arr), cmp_dbl() ) ; }
    catch( const std::exception& e )
    {
        std::cerr << "*** error: " << e.what()
                  << "\nsort was aborted after " << cmp_dbl::cnt << " comparisons\n" ; }
}

http://coliru.stacked-crooked.com/a/cbdca9e0f4844267

> My another idea of course, is to create a sorting algorithm or copy std::sort's code, and change it, with this extra

For anything more involved, that idea is the way to go.
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct BoundedSort
{
    bool operator ()(const int &lhs, const int &rhs)
    {
        if(abs(lhs - rhs) < 10)
        {
            return lhs < rhs;
        }

    }
}sort_by_bound;

auto main ()->int
{
    vector<int> v {1, 2, 4, 5, 3, 21, 4, 6, 9, 10};
    sort(v.begin(), v.end(), sort_by_bound);

    for (auto& elem : v)
    {
        cout << elem << " ";
    }
}

Output
1
2
3
4 21 1 2 3 4 5 6 9 10
Process returned 0 (0x0)   execution time : 0.719 s
Press any key to continue.

edit: OP: just seen JLBorges' and I want to stress once again what I have said before and s/he is saying as well, the container will be left in an unspecified state
Last edited on
Thanks for both.
What JLBorges said have sense.
gunnerfunner thanks, but you code contains errors and also not help me.
You also forget to return a value in case
abs(lhs-rhs)>=10

Daniel
Topic archived. No new replies allowed.