0

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 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];
1
  • Post some code of how you are populating your UIPickerView Commented Sep 29, 2011 at 14:56

2 Answers 2

2

Your array is already sorted by the index number

Sign up to request clarification or add additional context in comments.

3 Comments

+1 The order of elements in an array doesn't change spontaneously. You must be doing something to the array.
Yes the order is correct initially but when you scroll through the picker, the values come out of sequence because labels are getting reused and so if you scroll back and firth a few times we end up with the values viewed out of sequence which is messy
To avoid this I need a method of sorting specifically by index number, so the values stay visually organised even when reusing the labels in the picker
0

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.

3 Comments

Even the values look numerical, they are saved as strings because later in my plist I have values with alphabetical letter and some with symbols
In each case if I keep the sort order as entered in the plist it will avoid lots of problems so is there a method to sort by index number 01234 etc etc
If the values are saved in the array as strings they will be sorted as strings: 1,1-1/14,1-3/8,1/2,1/4,3/4,3/8. Sort is by content. If you want to save the entry order keep the original array, copy and sort that array anyway that is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.