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! :)

5
  • What did you try? Did ever try to find solution in web? There are a lot of tutorials and basics on sorting algorithms. Commented Nov 7, 2012 at 7: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. Commented Nov 7, 2012 at 7:15
  • They show what is not working. It's Q&A site. Commented Nov 7, 2012 at 7:17
  • 1
    This has been asked before: 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.) Commented Nov 7, 2012 at 7: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. :) Commented Nov 7, 2012 at 13:55

2 Answers 2

4

Use sort from the standard <algorithm> header:

std::sort(arrayOfProcesses, arrayOfProcesses+numProcesses, [](Process const &a, Process const &b){ return a.arrival < b.arrival; });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I ended up doing this: sort(arrayOfProcesses, arrayOfProcesses+numProcesses, sortOnArrival); I wrote my own sort function.
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.