I have an array containing custom objects with a property named seat
. Seat can have values 1A, 1D , 1C , 1k , 2A, 2k, 2D, 2C.
Now these can be arranged in any order, and I want to sort them according to class, however the sorting only accounts for the seats numeric value and not A, C, D,or K.
I want the order to be 1A,1C,1D,1K and so on.
This is what I have implemented in the SeatDO object:
-(NSComparisonResult) compareBySeatNumber:(SeatDO*)other {
NSComparisonResult result = NSOrderedSame;
NSInteger seatNumber = [self.seat integerValue];
NSInteger otherSeatNumber = [other.seat integerValue];
if (seatNumber > otherSeatNumber) {
result = NSOrderedDescending;
} else if (seatNumber < otherSeatNumber) {
result = NSOrderedAscending;
}
return result;
}
How do I make it consider the letters as well..?
To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.
In the main () method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the property, we use list 's sortedWith () method. The sortedWith () method takes a comparator compareBy that compares customProperty of each object and sorts it.
There are two ways you can sort an object array by a specific property, using Array.Sort () method and by using LINQ query. The people array in the above example contains objects of Person class. You cannot use Array.Sort (people) because array contains objects, not primitive values.
For sorting the list with the given property, we use list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator. and finally returns positive number if o1's property is greater than o2's, negative if o1's property is lesser than o2's, and zero if they are equal.
The sorted list is then stored in the variable sortedList. Here's the equivalent Java code: Java program to sort an ArrayList of custom objects by property.
Assuming that you convert the seat numbers to NSString
s
NSArray *sortedSeats = [seats sortedArrayUsingSelector:@selector(localizedStandardCompare:)]
should do the trick. Sorting strings will naturally follow the sort order you need.
Otherwise you could just use strings during the comparison with
[seats sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 stringValue] localizedStandardCompare:[obj2 stringValue]];
}];
I assumed that stringValue
is available for you custom object. If not, simply replace it with anything that will return a NSString
description of your instances.
NOTE
As suggested by Alladinian, you want to use localizedStandardCompare:
as opposed to caseInsensitiveCompare:
, in order to the get the proper lexicographic order.
Use localizedStandardCompare:
as the selector (Finder-like sorting)
[seats sortedArrayUsingSelector:@selector(localizedStandardCompare:)]
While caseInsensitiveCompare:
might seem like correct, if you add a @"10D"
or @"01C"
seat it would appear in front of all others...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With