Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. void sortByName(string theList[], int &namesInList) {
  2.     string temp1;
  3.     string temp2;
  4.     for(int j = 0; j < namesInList; j++) {
  5.  
  6.         for(int i = 0; i < namesInList * 2 -2; i = i + 2) { // Minus -2 på namesInList*2 så att loopen inte jämför de sista cellerna med nollcellerna som markerar slutet.
  7.             string firstNameI1, lastNameI1, fullNameI1, firstNameI3, lastNameI3, fullNameI3;
  8.             fullNameI1 = theList[i+1];
  9.             firstNameI1 = nameFinder(fullNameI1, true);
  10.             lastNameI1 = nameFinder(fullNameI1, false);
  11.             fullNameI3 = theList[i+3];
  12.             firstNameI3 = nameFinder(fullNameI3, true);
  13.             lastNameI3 = nameFinder(fullNameI3, false);
  14.             bool moveIt;
  15.             if(lastNameI1 == lastNameI3) {
  16.                 moveIt = isName1BiggerThanName2(firstNameI1,firstNameI3);
  17.             }
  18.             else {
  19.                 moveIt = isName1BiggerThanName2(lastNameI1,lastNameI3);
  20.             }
  21.             if(moveIt) {
  22.                 temp1 = theList[i];
  23.                 temp2 = theList[i+1];
  24.                 theList[i] = theList[i+2];
  25.                 theList[i+1] = theList[i+3];
  26.                 theList[i+2] = temp1;
  27.                 theList[i+3] = temp2;
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. string nameFinder(string fullName, bool firstOrLast) {
  34.  
  35.     string name;
  36.     int partingPosition = fullName.find("|");
  37.     if(firstOrLast) {
  38.         name = fullName.substr(0,partingPosition);
  39.         return name;
  40.     }
  41.     else {
  42.     int startPos4LN, endPos4LN;
  43.     startPos4LN = partingPosition + 1;
  44.     endPos4LN = fullName.length();
  45.     name = fullName.substr(startPos4LN,endPos4LN);
  46.     return name;
  47.     }
  48. }
  49.  
  50. bool isName1BiggerThanName2 (string name1, string name2) {
  51.     bool yesOrNo = 0;
  52.     bool whichIsLonger = 0;
  53.     int name1L = name1.length();
  54.     int name2L = name2.length();
  55.     if(name1L > name2L) {
  56.     bool whichIsLonger = 1;
  57.     }
  58.     else {
  59.     bool whichIsLonger = 0;
  60.     }
  61.     if (whichIsLonger) {
  62.         for(int i = 0; i < name2L; i++) {
  63.             if(name1[i] > name2[i]) {
  64.                 yesOrNo = 1;
  65.                 return yesOrNo;
  66.             }
  67.             if(name1[i] < name2[i]) {
  68.                 yesOrNo = 1;
  69.                 return yesOrNo;
  70.             }
  71.         }
  72.     }
  73.  
  74.     if (!whichIsLonger) {
  75.         for(int i = 0; i < name1L; i++) {
  76.             if(name1[i] > name2[i]) {
  77.                 yesOrNo = 1;
  78.                 return yesOrNo;
  79.             }
  80.             if(name1[i] < name2[i]) {
  81.                 yesOrNo = 1;
  82.                 return yesOrNo;
  83.             }
  84.         }
  85.     }
  86.     return yesOrNo;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement