Does anyone know how I could maintain relations between the char and int in structs while merge sorting the ints in the struct? So if I had nameandnumber[1].buffer assigned a value that used to be at nameandnumber[7].buffer, nameandnumber[7].phrase would get moved to nameandnumber[1].phrase?

My struct

typedef struct
    int buffer;
    char phrase[30];

and then I put

nameandnum *nameandnumber;

later in main

I'm parsing a series of numbers that correspond with letters in a .txt file that sort of looks like this:

  • 4 apple

  • 2 pear

  • 14 spaghetti

  • 18 tea

  • 1 coffee

so the numbers get put into buffer and the letters + spaces go into the phrase.

I'm merge sorting the numbers in buffer but how do I do that so 1 is still next to coffee rather than apple?

1 Answer 1


each instance of nameandnum is a complete object, when you do your merge sort you need to swap the whole objects not just the buffer field.

  • why did you unaccept the answer?
    – UpAndAdam
    Dec 4, 2023 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .