Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort an array of custom objects on this property?

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..?

like image 454
Ankit Srivastava Avatar asked Jun 18 '13 07:06

Ankit Srivastava


People also ask

How do you sort and array of objects?

To sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.

How to sort a list of custom objects with different properties?

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.

How to sort an array by a specific property in Java?

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.

How to sort the list with the given property in JavaScript?

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.

Where is the sorted list stored in Java?

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.


2 Answers

Assuming that you convert the seat numbers to NSStrings

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.

like image 145
Gabriele Petronella Avatar answered Sep 19 '22 05:09

Gabriele Petronella


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...

like image 29
Alladinian Avatar answered Sep 20 '22 05:09

Alladinian