Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stack>
- #include <vector>
- #include <sstream>
- #include <map>
- #include <iterator>
- #include <unordered_set>
- using namespace std;
- static bool customSort(string str1, string str2) {
- int pos1 = 0, pos2 = 0;
- string tempStr1 = "", tempStr2 = "";
- pos1 = str1.find(' ');
- tempStr1 = str1.substr(0, pos1);
- str1 = str1.substr(pos1+1, str1.size()-pos1-1);
- pos2 = str2.find(' ');
- tempStr2 = str2.substr(0, pos2);
- str2 = str2.substr(pos2+1, str2.size()-pos2-1);
- int i=0, j=0;
- while((i < str1.size()) && (j < str2.size())) {
- if(tolower(str1[i]) != tolower(str2[j]))
- return tolower(str1[i]) < tolower(str2[j]);
- i++;
- j++;
- }
- if(i != str1.size())
- return 0;
- if(j != str2.size())
- return 1;
- i = 0;
- j = 0;
- while((i < tempStr1.size()) && (j < tempStr2.size())) {
- if ((tempStr1[i]-'0' >= 0) && (tempStr1[i]-'0' <= 9)) {
- if ((tempStr2[j]-'0' >= 0) && (tempStr2[j]-'0' <= 9)) {
- if (tempStr1[i]-'0' != tempStr2[j]-'0')
- return tempStr1[i]-'0' < tempStr2[j]-'0';
- } else {
- return 1;
- }
- } else {
- if ((tempStr2[j]-'0' >= 0) && (tempStr2[j]-'0' <= 9)) {
- return 0;
- } else {
- if(tolower(tempStr1[i]) != tolower(tempStr2[j]))
- return tolower(tempStr1[i]) < tolower(tempStr2[j]);
- }
- }
- i++;
- j++;
- }
- return 1;
- }
- vector<string> reorderData(vector<string> myVec) {
- vector<string> res(myVec.size());
- vector<string> numRes;
- vector<string> alphRes;
- int pos = 0;
- string str = "";
- for(int i=0; i<myVec.size(); i++) {
- str = myVec[i];
- pos = str.find(' ');
- str = str.substr(pos+1, str.size()-pos-1);
- if((str[0]-'0' >= 0) && (str[0]-'0' <= 9))
- numRes.push_back(myVec[i]);
- else
- alphRes.push_back(myVec[i]);
- }
- sort(alphRes.begin(), alphRes.end(), customSort);
- copy(alphRes.begin(), alphRes.end(), res.begin());
- //vector<string> ::reverse_iterator r_it = res.rbegin();
- copy(numRes.begin(), numRes.end(), res.rbegin());
- return res;
- }
- int main()
- {
- vector<string> vec = {"al 9 2 3 1",
- "g1 Act car",
- "zo4 4 7",
- "abl off KEY dog",
- "a8 act zoo",
- "g1 act Car ban"};
- vector<string> res = reorderData(vec);
- for(int i=0; i<res.size(); i++) {
- cout << res[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement