4

Possible Duplicate:
c++ sort with structs

I am trying to figure out how to sort an array of structs on a specific variable in the struct held within the array. Here is my code:

struct Process{
    int pid;
    int burst;
    int arrival;
};

int main(int argc, char *argv[]){

    // The number of processes
    int numProcesses = 3;

    //Create an array that holds 10 Process structs
    Process *arrayOfProcesses = new Process[numProcesses];

    // Puts values in each pid, burst, and arrival
    arrayOfProcesses[0].pid = 0;
    arrayOfProcesses[0].burst = 8;
    arrayOfProcesses[0].arrival = 2;

    arrayOfProcesses[1].pid = 1;
    arrayOfProcesses[1].burst = 12;
    arrayOfProcesses[1].arrival = 3;

    arrayOfProcesses[2].pid = 2;
    arrayOfProcesses[2].burst = 4;
    arrayOfProcesses[2].arrival = 1;

    // Sort the array based on the arrival time
    // Help! :)
}

I would really like to be able to sort the array in my code on the arrival time. I've simplified my code to give you a general idea of what I'm trying to accomplish. In my actual code,t he array is filled dynamically from information read in by a file. I know that using a List or even a Vector would be better options, but I am determined to figure this out using arrays.

Any help with sorting this would be appreciated! :)

Community
  • 1
  • 1
Rick_Sch
  • 456
  • 3
  • 10
  • 19
  • What did you try? Did ever try to find solution in web? There are a lot of tutorials and basics on sorting algorithms. – Denis Ermolin Nov 07 '12 at 07:13
  • @DenisErmolin Hey! I've tried a variety of things. I seem drawn to a function I've found called qsort(), but I can't seem to make it work with what I'm doing. – Rick_Sch Nov 07 '12 at 07:15
  • They show what is not working. It's Q&A site. – Denis Ermolin Nov 07 '12 at 07:17
  • 1
    This has been asked before: http://stackoverflow.com/questions/873715/c-sort-with-structs (The accepted answer assumes a `std::vector`, but the question assumes an array, and some of the answers address this correctly.) – jogojapan Nov 07 '12 at 07:28
  • @jogojapan - Thank you! I actually did see that exact post, so thank you for digging deeper. I was going for more of a standard array using qsort, but as Juraj Blaho showed me below, I can still use the STL sort algorithm with standard arrays. You have all bee incredibly helpful. :) – Rick_Sch Nov 07 '12 at 13:55

2 Answers2

4

Use sort from the standard <algorithm> header:

std::sort(arrayOfProcesses, arrayOfProcesses+numProcesses, [](Process const &a, Process const &b){ return a.arrival < b.arrival; });
Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
  • Thank you! I ended up doing this: `sort(arrayOfProcesses, arrayOfProcesses+numProcesses, sortOnArrival);` I wrote my own sort function. – Rick_Sch Nov 07 '12 at 07:25
3

You can still use your array with STL sort algorithm by adding a compare function:

#include <algorithm>
bool operator<(const Process& lhs, const Process& rhs)
{
  return lhs.pid < rhs.pid;
}

sort(arrayOfProcesses, arrayOfProcesses + numProcesses);
billz
  • 44,644
  • 9
  • 83
  • 100