2012-03-01 15 views
58

मेरे पास शब्दकोशों द्वारा आबादी वाला एक सरणी है, और मुझे शब्दकोशों की कुंजी में से किसी एक के मूल्यों से वर्णमाला को क्रमबद्ध करने की आवश्यकता है।शब्दकोशों में एक कुंजी के मूल्य से शब्दकोशों के एनएसएआरएआर को छंटाई

यह मेरा सरणी है:

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; 
} 
) 

आम तौर पर एक सरणी छँटाई के लिए मैं

myArray = [uniqueProdsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

का उपयोग करेगा लेकिन यह कैसे प्रकार शब्दकोश की brand कुंजी का उपयोग करते हैं?

उत्तर

105

मुझे लगता है कि यह यह करना होगा:

brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; 
sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; 
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; 

मैं Sort Descriptor Programming Topics से कोड खींच लिया। इसके अलावा, Key-Value Coding खेल में आता है, उस sortedArrayUsingDescriptors: में myArray में प्रत्येक तत्व के लिए valueForKey: भेज देगा, और फिर लौटाए गए मानों को क्रमबद्ध करने के लिए मानक तुलनाकर्ताओं का उपयोग करें।

+0

स्विफ्ट 2 में इसका उपयोग कैसे करें? –

1

बस इसे बाहर सबसे आसान तरीका है की कोशिश ...

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]; 
1

आप ऐसा कर सकते हैं।

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]]; 
8

हम विधि का उपयोग कर समाधान मिल गया

[self.jsonData sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:"fullname" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:"id" ascending:NO], nil]]; 

अनुसरण करता है कहां: -

jsonData - MutableArray कौन सा पार्स किया गया JSON डेटा रखती है।

fullname - वह डेटा जिसे हम सॉर्ट करना चाहते हैं।

id - एक अद्वितीय डेटा जो आंतरिक शब्दकोश के साथ आता है।

1
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 
3
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; 
     }]; 
+0

सॉर्ट किया गया हैअरेयूउंगिंग कॉम्पैटरेटर जाने का तरीका है, "ऑब्जेक्टफोरकी" को दो कॉलों के बजाय "valueForKey" में चार कॉल तक बुरा है। Btw। चूंकि आप शब्दकोशों की अपेक्षा करते हैं, इसलिए NSDictionary * को प्रकार के रूप में उपयोग करें, आईडी नहीं। – gnasher729

3

QED के कोड में एक अतिरिक्त के रूप में,

NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; 
NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:@[brandDescriptor]]; 

यह चर की कक्षाओं को स्पष्ट करता है और तेजी से गणना के साथ सरणी निर्माण का अनुकूलन। धन्यवाद

6

switf में:

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "brand", ascending: true) 
var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor]) 
4

उपयोग निम्नलिखित यदि आप करने के लिए तरह "ब्रांड" शब्दकोश से कुंजी का उपयोग कर के लिए कोड .. निम्न कोड

NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES]; 
NSArray * sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; 
NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; 
NSLog(@"sortedArray %@",sortedArray); 

का प्रयोग करें, छँटाई दो चाबियाँ अनुसार शब्दकोश से; की तरह, "ब्रांड" कुंजी और शब्दकोश से productTitle कुंजी: -

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); 
2

जब NSSortDescriptor का उपयोग कर मेरे कोड दुर्घटनाग्रस्त हो गया था तो एक ब्लॉक जो अपने प्रयोग के मामले में, जहां मैं "रैंक" उम्मीद कर रहा हूँ में अच्छा काम करता है का उपयोग कर समाप्त हो गया एक NSNumber होने के लिए।यदि ऑब्जेक्ट को पूर्णांक में परिवर्तित नहीं किया जा सकता है तो यह सॉर्ट नहीं होगा लेकिन यह भी क्रैश का कारण नहीं बनता है।

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; 
}];