How to arrange names in aplhabatical order in c++ ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to arrange names in aplhabatical order in c++ ???

I am making a structure of food items and I want to know the code to how we can sort them alphabetically

25th Jan 2018, 9:23 AM
Harsh Sharma
Harsh Sharma - avatar
1 Answer
+ 6
string food1 = "", food2 = ""; for(int i = 0; i< length;i++) { food1 = food[i].name; for(int j = i+1; j< length;j++) { food2 = food[j].name; if(food1[i] > food2[j]) { temp = food1[i]; food1[i] = food2[j]; food2[j] = temp; } } }
25th Jan 2018, 7:03 PM
Vukan
Vukan - avatar