Sorting an array in objective-c [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sorting in nsmutable array
I have 2 NSMutableArray array. I have to sort it. Is there any function defined in Objective-c for direct sort.
like
NSMutableArray *sortedarray_variable= (sorting method) array_variable;
Where sorting method returns sorted array.

Of course. NSArray provides several methods

Related

Randomly select from an array of strings in Objective-C? [duplicate]

This question already has answers here:
Picking a Random Object in an NSArray
(8 answers)
Closed 3 years ago.
I have done some digging, and I don't believe this question has been answered yet (though I would imagine it would be simple).
I want to select a random string from an array. For example:
NSArray *strings = #[#"String1", #"String2", #"String3"];
NSString *randomString = // how to randomly select one of the three strings in the array?
You want arc4random_uniform():
NSString *randomString = strings[arc4random_uniform(strings.count)];
More info in this SO answer.

How do I query an NSArray when I know the attribute name and value? [duplicate]

This question already has answers here:
How to quickly search an array of objects in Objective-C
(4 answers)
NSPredicate for property of object in NSArray of NSArray
(1 answer)
Iterate through custom objects in a UIView and find matching properties
(2 answers)
Closed 5 years ago.
I am having a hard time getting the correct query type in my NSArray search, I tried the following which failed because its not searching for the correct value
BOOL isCashTender = [_tenderRows containsObject: #"Cash"];
I want to search inside the NSArray where there is a type attribute that contains the value Cash
I don't care how many rows/objects are inside the array I simply need to know if there is at least one type attribute containing the value Cash in there

Creating a Map<Object,Array<Object>> in objective c [duplicate]

This question already has answers here:
What is the Java equivalent of Objective-C's NSDictionary?
(3 answers)
Closed 9 years ago.
I am interested in creating the java equivalent of a Map data structure that looks as follows:
Map <Object ---> NSMutableArray of objects>
or
Map<Object,Array<Object>>
Can anyone provide guidance on what would be the best way of doing this in objective c as I am fairly new to the language.
Objective-c does not have typed collections. You just create NSMutableDictionary instance, and put NSMutableArray into the values.

Objective-C - sort array of strings based on string slice [duplicate]

This question already has answers here:
How to sort a NSArray alphabetically?
(7 answers)
How to do a natural sort on an NSArray?
(5 answers)
Closed 9 years ago.
NSMutableArray looks like ("xxx_20", "xxx_18", "xxx_16", "xxx_19", "xxx_17")
I want to sort it into ("xxx_16", "xxx_17", "xxx_18", "xxx_19", "xxx_20")
I've tried to use selectors, but I can't even get proper syntax. Does anyone know how to do this properly?
Try this,
NSMutableArray *myarray = [ NSMutableArray arrayWithObjects:#"xxx_20", #"xxx_18", #"xxx_16", #"xxx_19", #"xxx_17", nil];
NSArray *sortedArray = [myarray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog(#"%#",sortedArray);
It worked fine at my end.

How to slice NSMutableArray [duplicate]

This question already has an answer here:
How to slice an NSArray in Objective-C?
(1 answer)
Closed 8 years ago.
I have a NSMutableArray and need objects [0:5] only. Is there a simple way to slice? Can I drop all objects after index? Can I copy a sub-array to another NSMutableArray?
Use the instance method - (NSArray *)subarrayWithRange:(NSRange)range.
For example:
NSArray* slicedArray = [wholeArray subarrayWithRange:NSMakeRange(0, 5)];
I see James Bedford already answered how to extract a range of indexes. To delete the objects in a range from an NSMutableArray, you can use [wholeArray removeObjectsInRange:...]. To delete all objects after a particular index, you can create an appropriate range as NSMakeRange(index, wholeArray.count - index).