In: Computer Science
Say I have and Array so MyArray starts off as S A Q W A R .... I want to sort the Array alphabetically using ascii codes to sort. However in Ascii code capital letters come first and then lower case letters are sorted after. My goal is to not only sort the array, but also make lowercase sorted in its proper place. I am using visual basics and I cant use a built in sorter that does it the easy way, I have to sort it somehow in visual basics. please help me learn how todo this! Will rate very highly, thanks so much!
Hi According to your requirement i have written the code. It will sort the array content exactly as per your query.
Code:
#define CLASS_SIZE 10
#include <stdio.h>
void sortingCharArray(const char a[], char * b[]);
int main(void){
int i;
// initialize array
char * sortingOperationArray[CLASS_SIZE];
char myArray[CLASS_SIZE] =
{'a','r','p','b','r','c','x','e','w','j'};
// sort array
sortingCharArray(myArray,sortingOperationArray);
// print sorted array
for (i=0;i<CLASS_SIZE;i++){
printf("%c\n", *sortingOperationArray[i]);
}
return 0;
}
void sortingCharArray(const char a[], char * b[]){
char * temp;
int i,j;
// initialize b array to hold pointers to each element in
a
for (i=0;i<CLASS_SIZE;i++){
b[i] = (char *)(a) + i;
}
// in-place sort the b array
for(i=0;i<CLASS_SIZE;i++){
for(j=i+1;j<CLASS_SIZE-1;j++){
if(*b[j-1]>*b[j]){
temp = b[j];
b[j] = b[j-1];
b[j-1] = temp;
}
}
}
}