vectorbase::Sort

Description

Sort elements of the vector.

Syntax

BOOL Sort( int wOption = SORT_ASCENDING, BOOL bMissingValuesSmall = TRUE, vector<uint> & vnIndices = NULL, SORTCNTRL cntrl = 0 )

Parameters

wOption
[input] Set the sorting order and way. Default is SORT_ASCENDING.
Combination of the following bits:
SORT_DESCENDING, // descending sort
SORT_ASCENDING, // ascending sort
SORT_CASE_SENSITIVE, // option for string type vector, use case sensitive compare for string
SORT_NUMERIC_SUFFIX, // option for string type vector, will use numeric suffix to compare for string
SORT_CHECK_COMPARE_AS_NUMERIC, // convert text to numeric before comparing
};
bMissingValuesSmall
[input] Flag that specifies whether missing values are considered to be the smallest or largest value in the sorted result. Default is TRUE.
vnIndices
[output] Optional array to hold the original indices that corresponds to the sorted values.
cntrl
[input] Optional control bit flag:
SORTCNTRL_STABLE_ALGORITHM, // sort with stable algorithm.
SORTCNTRL_GET_SORTED_INDICES_ONLY, // not change source vector after sort, only output the sorted indices to vnIndeces.

Return

Returns TRUE on success and FALSE on error.

Examples

EX1

// Acending sort with default
void vectorbase_Sort_ex1()
{
        vector<int> vI = {1, 8, 5, 3};
        vI.Sort();
        for(int ii = 0; ii < vI.GetSize(); ii++)
                out_int("", vI[ii]); 
        // output should be 1, 3, 5, 8
}

EX2

//Use numeric suffix to sort string type vector
void vectorbase_Sort_ex2()
{
    vector<string> vs = { "a1", "a2", "a3","a10","a11", "a100"};
    
    // Use numeric suffix to compare for string.
    vs.Sort(SORT_DESCENDING|SORT_NUMERIC_SUFFIX);
    for(int ii = 0; ii < vs.GetSize(); ii++)
        out_str(vs[ii]);
    // result will be: a100, a11, a10, a3, a2, a1
   
    // Don't use numeric suffix to compare for string.
    vs.Sort(SORT_DESCENDING);
    for(int jj = 0; jj < vs.GetSize(); jj++)
        out_str(vs[jj]);
        // result will be: a3, a2, a11, a100, a10, a1
}

EX3

//Convert text to numeric to sort string type vector
void vectorbase_Sort_ex3()
{
    vector<string> vs = { "1", "2", "2.1","3","3.2"};
    
    // Convert text to numeric before sort
    vs.Sort(SORT_ASCENDING|SORT_CHECK_COMPARE_AS_NUMERIC);
    for(int ii = 0; ii < vs.GetSize(); ii++)
        out_str(vs[ii]);
    // result will be: 1, 2, 2.1, 3, 3.2
}

EX4

// Sort vector ascending, placing missing values at the end.
// Call this function with a worksheet active, where
// 1st column has some data with missing values in between data,
// and 2nd column is empty

void vectorbase_Sort_ex4()
{
    // Declare datasets and check validity
    Worksheet wks = Project.ActiveLayer();
    Dataset dsA(wks, 0);
    Dataset dsB(wks, 1);
    if( dsA && dsB )
    {
        // Copy dsA to a vector 
        vector vec(dsA);
        // Sort vector asending, placing missing values at the end
        vec.Sort(SORT_ASCENDING, FALSE);
        // Place sorted result in dsB
        dsB = vec;
    }
    else
        out_str("Failed to attach dataset objects to worksheet columns")                
}

EX5

//Sort with stable algorithm
void vectorbase_Sort_ex5(bool bStableAlgorithm = false)   //Change bStableAlgorithm to false
{     
        vector vData = {0, 2, 4, 6, 8, 10, 0, 2, 4, 6, 8, 10};
        
        SORTCNTRL nSortCtrl = 0;
        if( bStableAlgorithm )
                nSortCtrl = SORTCNTRL_STABLE_ALGORITHM;
        
        vector<uint> vn;
        vData.Sort(SORT_ASCENDING, true, vn, nSortCtrl);
        
        printf("Sort result:\n");
    for(int ii = 0; ii < vData.GetSize(); ii++)
        printf("%g  ", vData[ii]);
    
    printf("\t\n");
    printf("Index order:\n");    
    for(int jj = 0; jj < vn.GetSize(); jj++)
        printf("%d  ", vn[jj]);
        //bStableAlgorithm = true, 
        //Sort result:
        //0  0  2  2  4  4  6  6  8  8  10  10    
        //Index order:
        //0  6  1  7  2  8  3  9  4  10  5  11 

        //bStableAlgorithm = false,
        //Sort result:
        //0  0  2  2  4  4  6  6  8  8  10  10    
        //Index order:
        //0  6  1  7  8  2  9  3  10  4  5  11 
}

EX6

//Do not change source vector after sort, only output the sorted indices to vnIndex
void vectorbase_Sort_ex6()
{
    vector<double> vD = {1.5, 2.0, 3.3, 1.5, 1.5 };
    vector<uint> vnIndex;

    vD.Sort(SORT_ASCENDING, true, vnIndex, SORTCNTRL_GET_SORTED_INDICES_ONLY);
    
    printf("Sort result:\n");
    for(int ii = 0; ii < vD.GetSize(); ii++)
        printf("%g  ", vD[ii]);
    
    printf("\r\n");
    printf("Index order:\n");
    
    for(int jj = 0; jj < vnIndex.GetSize(); jj++)
        printf("%d  ", vnIndex[jj]);
        //Sort result:
        //1.5  2  3.3  1.5  1.5 
        //Index order:
        //0  3  4  1  2 
}

EX7

// shows how to sort two string vectors based on a third string vector 
void vectorbase_Sort_ex7()
{
    vector<string> vsCompositeNames, vsFiles;
    int nNumFiles = okutil_find_files_from_map(&vsCompositeNames, &vsFiles, SUPPORTFILE_XF);

    int ii;
    vector<string> vsNames;
    for(ii = 0; ii < vsFiles.GetSize(); ii++)
        vsNames.Add(GetFileName(vsFiles[ii], true));
    
    vector<uint> vn;
    vsNames.Sort(SORT_ASCENDING, true, vn);// sorting the file names
    // reorder the two vectors based on the sort result
    vsCompositeNames.Reorder(vn);
    vsFiles.Reorder(vn);
    
    for(ii = 0; ii < nNumFiles; ii++)
    {
       printf("%s\n   %s\n", vsCompositeNames[ii], vsFiles[ii]);
    }
}

Remark

Sort the elements of the vectorbase derived object, storing original indices in a separate vector. Currently not implemented for objects of type complex.

See Also

vectorbase::Reorder

Header to Include

origin.h