Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "," @ "," @ "," @ "," @ "," @ "," @ ", nil]; Nsinteger count = [array count]; for (int i = 0, i < count; i++) {for (int j = 0; J < count-i-1, j + +) {//if ([[[Array OBJECTATINDEX:J] intvalue] > [[Array Objectatindex: (j + 1)] intvalue]) {//here must be objectatindex:j when using [array intvalue//] if ([[[[Array objectatindex:j] Compare:[array Objectatindex:j + 1]] = =-1) {//The whole must have a return value, -1,0,1, because the return value of compare Nscomparisonresult is a value of an enumeration type, so return a value if ([[Arra Y Objectatindex:j] Compare:[array objectatindex:j + 1] options:nsnumericsearch] = = 1) {//Ibid. potions Nsnumericsearch = 64, [Array exchangeobjectatindex:j Withobjectatindex: (j + 1)]; Here you can use the Exchangeobjectatindex: method to exchange two-position array elements. }}} for (NSString *i in array) {NSLog (@ "%@", I); } NsmutableaRray *array1 = [Nsmutablearray arraywithobjects:@ "," @ "," @ "," @ "," @ "," @ "," @ ", nil]; [Array1 sortusingselector: @selector (compare:)]; NSLog (@ "%@", array);
Bubble sort in IOS (OC)