开发者

Sorting nsarray by index

开发者 https://www.devze.com 2023-04-10 00:12 出处:网络
I simply want to sort an NSArray by the index number i.e. The order in which the values are entered into the array.

I simply want to sort an NSArray by the index number i.e. The order in which the values are entered into the array.

My problem is that I use this array in a uipicker, and therefore when reusing labels, end up with my values in the wrong order

My values consist of fractions. 1/4,3/8,1/2,3/4,1,1-1/14,1-3/8 etc

I want these fractions to display in the order they are entered

Must be simple, but I am having no luck

When I use sorted array localisedstandardcompare all th开发者_C百科e values get out of sequence

Any help will be appreciated

  - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row    inComponent:(NSInteger)component
    {


    // Only calls the following code if component "0" has changed.
    if (component == 0) 
    {

        // Sets the global integer "component0Row" to the currently selected row of component "0"
        component0Row  = row;

        // Loads the new values for the selector into a new array in order to reload the data.
        NSDictionary *newDict = [[NSDictionary alloc]initWithDictionary:[pickerData objectForKey:[selectorKeysLetters objectAtIndex:component0Row]]];

        NSArray *sortArray = [[NSArray alloc]initWithArray:[newDict allKeys]];

        NSMutableArray *newValues = [[NSMutableArray alloc]initWithArray:[sortArray  sortedArrayUsingSelector:@selector(compare:)]];

        self.selectorKeysNumbers = newValues;

        component1Row = 0;
        [self.myPicker selectRow:0 inComponent:1 animated:NO];


Your array is already sorted by the index number


Assuming that the array values are NSStrings the sort order will be by string value, not numeric value. In order to sort these fractions (rational numbers) you would have to write you own compare function.

But see @hypercrypt, the array should already be in the order entries were made.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号