简体   繁体   中英

Sorting nsarray by index

I simply want to sort an NSArray by the index number ie 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 the 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];

您的数组已按索引号排序

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM