Ich habe ein Array mit Wörterbüchern gefüllt, und ich muss das Array alphabetisch nach den Werten eines der Schlüssel der Wörterbücher sortieren.
Dies ist mein Array:
tu dictus: (
{
brand = Ryul;
productTitle = Any;
quantity = 1;
subBrand = "Ryul INJ";
type = Product;
},
{
brand = Trol;
productTitle = Different;
quantity = 2;
subBrand = "";
type = Brand;
},
{
brand = Dtor;
productTitle = Any;
quantity = 1;
subBrand = "";
type = Product;
},
{
brand = Ryul;
productTitle = Different;
quantity = 2;
subBrand = "Ryul CHES";
type = SubBrand;
},
{
brand = Anan;
productTitle = Any;
quantity = 1;
subBrand = "";
type = Product;
}
)
Normalerweise verwende ich zum Sortieren eines Arrays
myArray = [uniqueProdsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Aber wie sortiere ich mit dem brand
Schlüssel des Wörterbuchs?
Ich denke das wird es schaffen:
brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
Ich habe den Code aus Sort Descriptor Programming Topics gezogen. Außerdem kommt Key-Value Coding ins Spiel, indem sortedArrayUsingDescriptors:
sendet ein valueForKey:
zu jedem Element in myArray
und sortieren Sie die zurückgegebenen Werte mit Standardkomparatoren.
Wir haben die Lösung mit der folgenden Methode erhalten
[self.jsonData sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:"fullname" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:"id" ascending:NO], nil]];
Wo:-
jsonData
- MutableArray, das die analysierten JSON-Daten enthält.
fullname
- die Daten, die wir sortieren möchten.
id
- Eindeutige Daten, die mit dem inneren Wörterbuch geliefert werden.
Verwenden Sie folgenden Code zum Sortieren mit dem "brand" -Schlüssel aus dem Wörterbuch.
NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedArray %@",sortedArray);
Verwenden Sie den folgenden Code, wenn Sie nach zwei Schlüsseln aus dem Wörterbuch sortieren möchten. Wie "Brand" -Schlüssel und ProductTitle-Schlüssel aus dem Wörterbuch: -
NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
NSSortDescriptor * productTitleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"productTitle" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObjects:brandDescriptor, productTitleDescriptor, nil];
NSArray * sortedArray = [feedData sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedArray %@",sortedArray);
arrSorted = [arrBrand sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([[obj1 valueForKey:@"iUserId"] integerValue] > [[obj2 valueForKey:@"iUserId"] integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[obj1 valueForKey:@"iUserId"] integerValue] < [[obj2 valueForKey:@"iUserId"] integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Im Handumdrehen:
var descriptor: NSSortDescriptor = NSSortDescriptor(key: "brand", ascending: true)
var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor])
Als Ergänzung zum QED-Code
NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:@[brandDescriptor]];
Dies verdeutlicht die Klassen der Variablen und optimiert die Array-Erstellung mit schneller Aufzählung. Vielen Dank
Mein Code stürzte bei der Verwendung von NSSortDescriptor
ab, so dass ein Block verwendet wurde, der in meinem Anwendungsfall, in dem ich einen "Rang" als NSNumber erwarte, hervorragend funktioniert. Wenn das Objekt nicht in eine Ganzzahl konvertiert werden kann, wird es nicht sortiert, aber es wird auch keinen Absturz verursachen.
NSArray *sortedArray = [data sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
long data1 = [[obj1 valueForKey:@"rank"] integerValue];
long data2 = [[obj2 valueForKey:@"rank"] integerValue];
if (data1 > data2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (data1 < data2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSSortDescriptor *brandDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Position" ascending:YES selector:@selector(localizedStandardCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
NSArray *sortedArray = [arrTemp sortedArrayUsingDescriptors:sortDescriptors];
array_PreLagData=(NSMutableArray*)sortedArray;
unsorted array
Printing description of arrTemp:
<__NSArrayM 0x10282100>(
{
Milker2 = "11:03:17 AM";
Position = 2;
},
{
Milker1 = "11:03:28 AM";
Position = 25;
},
{
Milker3 = "11:03:18 AM";
Position = 3;
},
{
Milker1 = "11:03:16 AM";
Position = 1;
Strip = "11:32:32 AM";
},
{
Milker1 = "11:03:21 AM";
Position = 10;
}
)
Sorted array
<__NSArrayI 0x101363c0>(
{
Milker1 = "11:03:16 AM";
Position = 1;
Strip = "11:32:32 AM";
},
{
Milker2 = "11:03:17 AM";
Position = 2;
},
{
Milker3 = "11:03:18 AM";
Position = 3;
},
{
Milker1 = "11:03:21 AM";
Position = 10;
},
{
Milker1 = "11:03:28 AM";
Position = 25;
}
)
[enter link description here][1]
[1]: https://developer.Apple.com/library/ios/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html#//Apple_ref/doc/uid/20001845-BAJEAIEE
du kannst das .
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"d LLLL yyyy"];
NSComparator compareDates = ^(id string1, id string2)
{
NSDate *date1 = [formatter dateFromString:string1];
NSDate *date2 = [formatter dateFromString:string2];
return [date1 compare:date2];
};
NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO comparator:compareDates];
[array sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1, nil]];
Probieren Sie es einfach aus ...
myArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray removeAllObjects];
[tempArray addObjectsFromArray: myArray];
NSString *key = @"brand";
NSSortDescriptor *brandDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:brandDescriptor,nil];
NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortDescriptors];
[brandDescriptor release];
[tempArray removeAllObjects];
tempArray = (NSMutableArray*)sortedArray;
[myArray removeAllObjects];
[myArray addObjectsFromArray:tempArray];
Verwenden Sie dies für Swift 4
let sortedArray = arrayTobeSort.sorted {$0["keyName"].compare($1["keyName"]) == ComparisonResult.orderedAscending}
Sie können auch ComparisonResult.orderedDescending verwenden, um in absteigender Reihenfolge zu sortieren