0

so I put the number and alphabet into an array and a string but they are related as in 10b must be 10b and 4b must be 4b.

I am hoping to sort the string with the number alphabetically from a to b to c... etc

The number with the ASCII odd alphabet should be in ascending order but the number with the ASCII even alphabet should be in descending order.

i.e. as the following case should be 5a 12a 10b 4b 4h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[]) {
    char *alpha[] = {"b", "b", "h", "a", "a"};
    int num[5]= {4,10,4,12, 5};
    int i;

    printf("The classes are ");

    for (i=0; i<5; i++){
    printf("%d%s ", num[i], alpha[i]);
    }
    printf("\n");

    printf("The classes are rearragnged to:");

    return 0;
}

Any idea on how should I sort them?

(Extra note: I tried bubble sort but doesn't really work with an array and string... I also put them both in a string as well but when sorting with 4b and 10b, it goes random as it cant compare the correct string position with a single digit with double digit...)

7
  • what are your expected output? Commented May 15, 2017 at 10:12
  • if 1a 2a 3a 4a 1b 3b ==> 1a 3a 4a 2a 1b 3b ? Commented May 15, 2017 at 10:18
  • 5a 12a 10b 4b 4h Commented May 15, 2017 at 10:34
  • 1
    if 1a 2a 3a 4a 1b 3b should be 1a 2a 3a 4a 3b 1b Commented May 15, 2017 at 10:34
  • "they are related as in 10b must be 10b and 4b must be 4b" Eeh? Commented May 15, 2017 at 10:54

1 Answer 1

1

If you treat alpha and num as pairs, the best way of expressing this is usually to combine them in one object (and then to sort these objects) rather than managing two separate arrays (which do not express this "pair"-relationship). If - for some reason - you are not allowed to change the data structure, then there are other ways as well (for which you can ask later, if you like). But let me propose here the solution that expresses the "pair"-intention:

struct classStruct {
    char *alpha;
    int num;
};

int compareClassStructByLetter(const void *c1, const void *c2) {
    struct classStruct* c1Ptr = (struct classStruct *)c1;
    struct classStruct* c2Ptr = (struct classStruct *)c2;
    int result = 0;
    int strcmpResult = strcmp(c1Ptr->alpha, c2Ptr->alpha);
    if (strcmpResult != 0) {
        result = strcmpResult;
    }
    else {
        result = c2Ptr->num - c1Ptr->num;   // ascending order...
        char c = *c1Ptr->alpha;
        if (c % 2) { // odd alpha? (for example, 'a'== 65 == odd)?
            result = -result;  // reverse order to descending
        }
    }
    return result;
}

int main(int argc, char *argv[]) {
    int i;
    struct classStruct classes[5] = {
        { "b", 4 },
        { "b", 10 },
        { "h", 4 },
        { "a", 12 },
        { "a", 5 }
    };

    printf("The classes are ");

    for (i=0; i<5; i++){
        printf("%d%s ", classes[i].num, classes[i].alpha);
    }
    printf("\n");

    qsort(classes, 5, sizeof(struct classStruct), compareClassStructByLetter);

    printf("The classes are rearragnged to:");
    for (i=0; i<5; i++){
        printf("%d%s ", classes[i].num, classes[i].alpha);
    }
    printf("\n");

    return 0;
}

The output of this program is then:

The classes are 4b 10b 4h 12a 5a 
The classes are rearragnged to:5a 12a 10b 4b 4h
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, it works! sometime it hard to know what kind of struct to use or not to use struct, anyway, thanks again =)
@sai_985 This does not work as expected. DEMO
Not very hard to fix, here (also made nicer for qsort())
@BLUEPIXY: adapted the comparison function;
@Hasturkun: cannot open the link; but adapted the comparison function; should work now as expected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.