I am trying to sort an array of strings, but it's not sorting anything.... what am I doing wrong?
string namesS[MAX_NAMES]; int compare (const void * a, const void * b){ return ( *(char*)a - *(char*)b ); } void sortNames(){ qsort(namesS, MAX_NAMES, sizeof(string), compare); }
To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.
It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order. Syntax: public static <T> Comparator<T> reverseOrder()
Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings.
In Bubble Sort, the two successive strings arr[i] and arr[i+1] are exchanged whenever arr[i]> arr[i+1]. The larger values sink to the bottom and hence called sinking sort. At the end of each pass, smaller values gradually “bubble” their way upward to the top and hence called bubble sort.
This is C++, not C. Sorting an array of strings is easy.
#include <string> #include <vector> #include <algorithm> std::vector<std::string> stringarray; std::sort(stringarray.begin(), stringarray.end());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With