Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void sortByName(string theList[], int &namesInList) {
- string temp1;
- string temp2;
- for(int j = 0; j < namesInList; j++) {
- 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.
- string firstNameI1, lastNameI1, fullNameI1, firstNameI3, lastNameI3, fullNameI3;
- fullNameI1 = theList[i+1];
- firstNameI1 = nameFinder(fullNameI1, true);
- lastNameI1 = nameFinder(fullNameI1, false);
- fullNameI3 = theList[i+3];
- firstNameI3 = nameFinder(fullNameI3, true);
- lastNameI3 = nameFinder(fullNameI3, false);
- bool moveIt;
- if(lastNameI1 == lastNameI3) {
- moveIt = isName1BiggerThanName2(firstNameI1,firstNameI3);
- }
- else {
- moveIt = isName1BiggerThanName2(lastNameI1,lastNameI3);
- }
- if(moveIt) {
- temp1 = theList[i];
- temp2 = theList[i+1];
- theList[i] = theList[i+2];
- theList[i+1] = theList[i+3];
- theList[i+2] = temp1;
- theList[i+3] = temp2;
- }
- }
- }
- }
- string nameFinder(string fullName, bool firstOrLast) {
- string name;
- int partingPosition = fullName.find("|");
- if(firstOrLast) {
- name = fullName.substr(0,partingPosition);
- return name;
- }
- else {
- int startPos4LN, endPos4LN;
- startPos4LN = partingPosition + 1;
- endPos4LN = fullName.length();
- name = fullName.substr(startPos4LN,endPos4LN);
- return name;
- }
- }
- bool isName1BiggerThanName2 (string name1, string name2) {
- bool yesOrNo = 0;
- bool whichIsLonger = 0;
- int name1L = name1.length();
- int name2L = name2.length();
- if(name1L > name2L) {
- bool whichIsLonger = 1;
- }
- else {
- bool whichIsLonger = 0;
- }
- if (whichIsLonger) {
- for(int i = 0; i < name2L; i++) {
- if(name1[i] > name2[i]) {
- yesOrNo = 1;
- return yesOrNo;
- }
- if(name1[i] < name2[i]) {
- yesOrNo = 1;
- return yesOrNo;
- }
- }
- }
- if (!whichIsLonger) {
- for(int i = 0; i < name1L; i++) {
- if(name1[i] > name2[i]) {
- yesOrNo = 1;
- return yesOrNo;
- }
- if(name1[i] < name2[i]) {
- yesOrNo = 1;
- return yesOrNo;
- }
- }
- }
- return yesOrNo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement