簡體   English   中英

按索引對nsarray進行排序

[英]Sorting nsarray by index

我只是想通過索引號對NSArray進行排序,即將值輸入到數組中的順序。

我的問題是我在一個uipicker中使用這個數組,因此當重用標簽時,最終我的值以錯誤的順序結束

我的價值觀包括分數。 1 / 4,3 / 8,1 / 2,3 / 4,1,1-1 / 14,1-3 / 8等

我希望這些分數按輸入順序顯示

一定很簡單,但我沒有運氣

當我使用排序數組localisedstandardcompare時,所有值都不按順序排列

任何幫助將不勝感激

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

您的數組已按索引號排序

假設數組值是NSStrings,排序順序將是字符串值,而不是數值。 為了對這些分數(有理數)進行排序,您必須編寫自己的比較函數。

但是看看@hypercrypt,數組應該已經在訂單條目中了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM