0

Here I have a structure of calls

typedef struct calls 
{
    string contact;
    string startTime;
    string endTime;
    
} callDetails;

vector <callDetails> details;

In my code i'm reading a file in to this vector.In that file there some details about calls(contact,call start time and end time)I've shown couple of line of that file below

Contact,Start Time,End Time
711256677,7,7.15
711345678,13,13.07
772345627,20,20.55

Here what i want to do is sort and display this call details on my program.I wrote a code to get the call duration(end time-start time) and sort it.But I cant display those sorted details properly.I want to display it in ascending order.also in this manner contact number , start time ,end time.Basically the same above shown table like structure but sorted according to the call duration.Below I've shown the code i wrote.

calls callDetails[maxNames];
    float callDuration[maxNames] , startMin[maxNames], endMin[maxNames] , temp;
    readAllCalls(callDetails);

    for (int i = 0 ; i < details.size() ; i++){
        startMin[i] = stof(details[i].startTime);
        endMin[i] = stof(details[i].endTime);
        callDuration[i] = (endMin[i] -  startMin[i]) - 0.4;
    }  
    for (int j = 0; j < (details.size() - 1); j++){
         for (int i = j + 1; i < details.size(); i++){
              if (callDuration[j] < callDuration[i]){
                  temp = callDuration[j]; 
                  callDuration[j] = callDuration[i];
                  callDuration[i] = temp;
              }
         }
    }

can someone help me with this?(Display the call details in the same table but sorted)

  • I don't understand where exactly your problem is. Maybe, it would help if you extracted a [mcve] from your code. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Aug 29 '20 at 09:58
  • @UlrichEckhardt Here I've sorted all calls by call duration.But i want to display these sorted items in a table like above.Thats my problem –  Aug 29 '20 at 10:04
  • @SeektheFreak2 So the part that is unclear is, if you have sorted the array successfully, why can't you display it in exactly the same way as an unsorted array? What's the difference between displaying a sorted array and displaying an unsorted array (there should be none). – john Aug 29 '20 at 10:06
  • @john Because i thought it would be easy to sort these details if I copy that data(Start time, end time and call duration) to different arrays –  Aug 29 '20 at 10:06
  • @SeektheFreak2 Well you got that the wrong way round. Because now you have several arrays to sort, in parallel, and that's harder than sorting one array. – john Aug 29 '20 at 10:07
  • @john When it's unsorted i'm directly displaying it from vector.But after just sorting the call duration(end time -start time ) i don't know how to display it properly –  Aug 29 '20 at 10:08
  • @john could you help me with a easier way please? –  Aug 29 '20 at 10:09
  • @SeektheFreak2 -- Please see [this answer](https://stackoverflow.com/questions/46382252/sort-array-by-first-item-in-subarray-c/46382976#46382976) and read the footnote to that answer. The bottom line is you create an index array and you sort that array. Then you use the index array to access the elements in a sorted manner. – PaulMcKenzie Aug 29 '20 at 10:24

1 Answers1

0

You should avoid sorting parallel arrays. Here's how to sort the original details array.

for (int j = 0; j < (details.size() - 1); j++){
    for (int i = j + 1; i < details.size(); i++){
        float startMin_j = stof(details[j].startTime);
        float endMin_j = stof(details[j].endTime);
        float callDuration_j = (endMin_j -  startMin_j) - 0.4;
        float startMin_i = stof(details[i].startTime);
        float endMin_i = stof(details[i].endTime);
        float callDuration_i = (endMin_i -  startMin_i) - 0.4;
        if (callDuration_j < callDuration_i){
              temp = details[j]; 
              details[j] = details[i];
              details[i] = temp;
        }
   }

}

This could be cleaned up. The code to calculate a code duration is duplicated, and should be moved to a separate function. But hopefully you get the idea, do all the calculations on the array you are sorting, trying to sort parallel arrays just makes it more complex not less.

john
  • 85,011
  • 4
  • 57
  • 81