Sorting vector of arrays with many elements (solved) [closed]

I have the followings:

vector<array<time_t,12>*> records;
void sorter(array<int,10> s)
{
    sort(records.begin(),records.end(),[&s](array<time_t,12>*& a,array<time_t,12>*& b)
    {return (*a)[s[0]]!=(*b)[s[0]]?(*a)[s[0]]<(*b)[s[0]]:(*a)[s[1]]!=(*b)[s[1]]?(*a)[s[1]]<(*b)[s[1]]
    :(*a)[s[2]]!=(*b)[s[2]]?(*a)[s[2]]<(*b)[s[2]]:(*a)[s[3]]!=(*b)[s[3]]?(*a)[s[3]]<(*b)[s[3]]
    :(*a)[s[4]]!=(*b)[s[4]]?(*a)[s[4]]<(*b)[s[4]]:(*a)[s[5]]!=(*b)[s[5]]?(*a)[s[5]]<(*b)[s[5]]
    :(*a)[s[6]]!=(*b)[s[6]]?(*a)[s[6]]<(*b)[s[6]]:(*a)[s[7]]!=(*b)[s[7]]?(*a)[s[7]]<(*b)[s[7]]
    :(*a)[s[8]]!=(*b)[s[8]]?(*a)[s[8]]<(*b)[s[8]]:(*a)[s[9]]!=(*b)[s[9]]?(*a)[s[9]]<(*b)[s[9]]:(*a)[2]<(*b)[2];});
}

I couldn't find a smart way to do this so I'm asking here. Can it be done like in one-liner somehow? Also I need to provide optional descending-ascending sorting. I mean adding an another array<bool,10> to sorter function as parameter and according to each value, the function should be able to decide doing sorting ascending or descending. I tried something like (!s[9]?<:>) but apparently operators cannot be included to short if-else statements. Or can they be?

@Edit: I added a array<bool,10> parameter to specify ascending and it works now like this:

void sorter(array<int,10> s,array<bool,10> asc)
{
    sort(records.begin(),records.end(),[&s,&asc](array<time_t,12>*& a,array<time_t,12>*& b)
    {return
    (*a)[s[0]]!=(*b)[s[0]]?asc[0]?(*a)[s[0]]>(*b)[s[0]]:(*a)[s[0]]<(*b)[s[0]]:(*a)[s[1]]!=(*b)[s[1]]?asc[1]?(*a)[s[1]]>(*b)[s[1]]:(*a)[s[1]]<(*b)[s[1]]
    :(*a)[s[2]]!=(*b)[s[2]]?asc[2]?(*a)[s[2]]>(*b)[s[2]]:(*a)[s[2]]<(*b)[s[2]]:(*a)[s[3]]!=(*b)[s[3]]?asc[3]?(*a)[s[3]]>(*b)[s[3]]:(*a)[s[3]]<(*b)[s[3]]
    :(*a)[s[4]]!=(*b)[s[4]]?asc[4]?(*a)[s[4]]>(*b)[s[4]]:(*a)[s[4]]<(*b)[s[4]]:(*a)[s[5]]!=(*b)[s[5]]?asc[5]?(*a)[s[5]]>(*b)[s[5]]:(*a)[s[5]]<(*b)[s[5]]
    :(*a)[s[6]]!=(*b)[s[6]]?asc[6]?(*a)[s[6]]>(*b)[s[6]]:(*a)[s[6]]<(*b)[s[6]]:(*a)[s[7]]!=(*b)[s[7]]?asc[7]?(*a)[s[7]]>(*b)[s[7]]:(*a)[s[7]]<(*b)[s[7]]
    :(*a)[s[8]]!=(*b)[s[8]]?asc[8]?(*a)[s[8]]>(*b)[s[8]]:(*a)[s[8]]<(*b)[s[8]]:(*a)[s[9]]!=(*b)[s[9]]?asc[9]?(*a)[s[9]]>(*b)[s[9]]:(*a)[s[9]]<(*b)[s[9]]
    :(*a)[2]<(*b)[2];}); //notice (*a)[2] is not the same as (*a)[s[2]] and it means default sorting
}

I know this is a huge mess but no need to shorten it since it's working perfectly. Thanks to people who helped me in the comments section.

@Edit: If someone gets curious on how I call this function:

array<int,10> s; s.fill(2); 
for(char i=0; i<10; i++)
    for(char j=0; j<12; j++)
        if(Button_GetCheck(GetDlgItem(hwnd,4+i*12+j))) s[i]=j;
array<bool,10> asc; asc.fill(0);
for(char i=0; i<10; i++)
    if(Button_GetCheck(GetDlgItem(hwnd,125+i))) asc[i]=1;
sorter(s,asc);

Checkboxes specify ascending. And here I sort the container according to its 3rd element by descending and 11th&10th elements by ascending orders. This example can clear things up and reveal the (crippled) logic behind it.



Read more here: https://stackoverflow.com/questions/66268619/sorting-vector-of-arrays-with-many-elements-solved

Content Attribution

This content was originally published by user814412 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: