Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ sort array of strings

Tags:

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); } 
like image 782
user69514 Avatar asked May 10 '10 13:05

user69514


People also ask

How do you sort an array of strings?

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.

How do you arrange an array of strings in ascending order?

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()

How do you sort an array by name?

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.

How do you sort an array of strings using bubble sort?

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.


1 Answers

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()); 
like image 173
Puppy Avatar answered Sep 27 '22 21:09

Puppy