I am getting values which are sorted from a plist and displaying them in a `tableview`

. I am providing the capability of entering a custom category which will be written to plist. But I want that to be Inserted in the sorted alphabetical order. Can someone suggest me how to get the `indexpath`

of the row where it has to be inserted. I have used the following code where the custom entry will be inserted at the 0 location

```
NSInteger section = 0;
NSInteger row = 0;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSDictionary *categoryDictionary = [NSMutableDictionary dictionary];
[categoryDictionary setValue:newCategoryName forKey:@"name" ];
[categoryDictionary setValue:@"custom.png" forKey:@"image"];
[[self categoriesArray]insertObject:categoryDictionary atIndex:row];
[[self categoriesArray] writeToFile:[self dataFilePath] atomically:YES];
NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];
[[self tableView]insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];
```

In your table you will get section and row using following

```
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
```

if you have an array of sortable items, you can use this code snippet as help:

```
NSArray *sortedArray = @[@"a" , @"b", @"d", @"c", @"f"];
sortedArray = [sortedArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"%@", sortedArray);
int c = [sortedArray indexOfObject:@"c"];
NSLog(@"%d", c);
```

Logs ->

```
(
a,
b,
c,
d,
f
)
2
```

so you first insert your object in the "toSort" array and then get the index of it ;)

Try

```
NSMutableDictionary *categoryDictionary = [NSMutableDictionary dictionary];
[categoryDictionary setObject:newCategoryName forKey:@"name" ];
[categoryDictionary setObject:@"custom.png" forKey:@"image"];
//Add new category to array
[self.categoriesArray addObject:categoryDictionary];
//Sort Array
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[self.categoriesArray sortUsingDescriptors:@[sortDescriptor]];
//Write array to plist
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];
NSInteger section = 0;
//Get the index of saved Category item
NSInteger row = [self.categoriesArray indexOfObject:categoryDictionary];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationRight];
```

Similar Questions

I am trying to get a URL from a cell. To do this, I am using NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; and then would like to do something like NSURL *url = self.finalURL[inde

I have a numerical list: myList = [1, 2, 3, 100, 5] Now if I sort this list to obtain [1, 2, 3, 5, 100]. What I want is the indices of the elements from the original list in the sorted order i.e. [0,

I'm adding a switch to contentView, and the contentView belongs to the cell. I'm trying to get the row number of the cell which has the switch triggered. Here's the code: - (UITableViewCell *)tableVie

I have three sorted array I need to find top five 5 element from these array .I am able to find first two element largest element .how I will find other ? can you suggest how we can find other 3 ele

There are 2 arrays given for ex. A = [20,4,21,6,3] B = [748,32,48,92,23......] assuming B is very large and can hold all the elements of array A. Find the way in which array B is in (containing all th

so i have an array (say @array) with sorted values between 0 and 1, and also and a hash (say %hash) whose keys are sorted and are numbers between 0 and 1. the values for each key in the hash is 0. Now

I know that the definition of suffix array itself is that its a sorted array of all the suffixes of a string. But I am trying to understand whats the significance of this sorting operation here? Suppo

I know there is a method called [tableView cellForRowAtIndexPath:indexPath]; which we can use to get cell at given indexPath.But i want to expand row when the row tapped.using this code- - (void)tab

Possible Duplicates: Merging two sorted lists Algorithm for N-way merge Given k sorted arrays each of length n, construct a single merged and sorted array.focus on running time and space complexity.

I know from the MS documentation that the Request.UserLanguages array is sorted. What they don't say is whether the array is sorted by priority or alphabetically. For example, if my Accept-Language

In the function below, I'm seeing to sort the array by alpha. However, it returns bool(true) rather than the actual sorted array. What am I missing? function get_dirs($dir) { $array = array(); $d = di

Find floor and ceil of a number X from an array which is already sorted. e.g. a[] = {3, 7, 9, 10, 15} if X=2, floor = N/A, ceil = 3 if X=3, floor = 3, ceil = 3 if X=4, floor = 3, ceil = 7 if X=16, f

Having two classes like Blog and Post, in Entity Framework (and LINQ-to-Entities), how can you get the blogs with the posts sorted by date. I was getting the blogs with the posts this way: from blog i

I am trying to write the following objective-c code in robovm: (NSIndexPath *)indexPath = ...; cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; However, when I look thru RoboVM's calls

Source : Microsoft Interview Question Given a sorted array, in which every element is present twice except one which is present single time, we need to find that element. Now a standard O(n) solution

In Programming Pearls (second edition) Column 5, problem 5 the question is about implementing binary search on an unsorted array. How can you add partial checking to the function at significantly les

Can anybody optimize following statement in Scala: // maybe large val someArray = Array(9, 1, 6, 2, 1, 9, 4, 5, 1, 6, 5, 0, 6) // output a sorted list which contains unique element from the array with

Given a Sorted Array which can be rotated find an Element in it in minimum Time Complexity. eg : Array contents can be [8, 1, 2, 3, 4, 5]. Assume you search 8 in it.

I was asked the below question in an interview: Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minim

I came across this problem - input - I am given two sorted arrays a1 and a2. I need to find the elements that are not present in the second array. I have two approaches 1) Hashtable - O(m+n) [use whe

I have a reference array which contains all possible elements and is sorted in custom order different than alphanumeric sorting. For example, @ref_array = ('one','two','three','four','five','six'); N

I have this array: $categories = array( array('id' => 1, 'parent' => 0, 'name' => 'Category A'), array('id' => 2, 'parent' => 0, 'name' => 'Category B'), array('id' => 3, 'parent'

i want to animate the UICollectionViewCell when action is called. i have done UICollectionViewCell in Interface Builder, the UICollectionView also. Now i want to get the correct indexPath at my action

How to get github search results sorted by the number of forks or stars? I need this feature to demonstrate which project is the hottest among those that target the similar task/function/feature. I no

I need to transform an array to html table sorted by time, add rank and calculate time loss for the first place. Array example:` var array = [ [McNulty Oscar, USA, 108:45.1], [Johansson Anton,

I am developing a Catalyst app which uses Template::Toolkit as template engine. One page needs a list of equal input elements. They can be taken from an array but I need both sort order and a descript

i have a table view, split into alphabetical sections, populated from array that has been filtered how can i track the true indexpath of cells in a grouped tableview. instead of the count starting a

I'm trying to make an algorithm that finds the best position to insert the target into the already sorted array. The goal is to either return the position of the item if it exists in the list, else re

I want to implement an algorithm that inserts sorted arrays into binary search trees but I don't want ending up with a tree that only grows to one side. Do you have any ideas? Thanks.

sorry I just started learning LINQ, and I would be glad if you would say it is possible to do using Linq sort this array by ID or BonusCount string[] res; // {ID,Name, BonusCount,etc} res = new stri

I have a table with sorted numbers like: 1 320102 2 5200100 3 92010023 4 112010202 5 332020201 6 332020411 : 5000000000 3833240522044511 5000000001 3833240522089999 5000000002 4000000000213312 Given

let us consider following code >> B=xlsread('data_generations1','A1','g8:g301'); >> [pxx,f]=periodogram(B,[],[],100); >> [peaks,location]=findpeaks(pxx); >> [sorted_peaks,i]=so

here i have wrote code for finding median of two sorted array #include<iostream> using namespace std; #define L 5 #define M 6 const int N=L+M; int A[1000];//define 1 indexed aarray int B[1000];

I have two arrays. I should do a normal sort on one (descending or ascending) and sort the other according to how the first array is sorted. This is because each element in the first array has a relat

I have an array of sorted integers, and I'd like to get the two consecutive indices of elements that bound a particular value I pass in. To illustrate, because it's hard to describe in words, let's sa

I was asked this question recently in an interview : What is the most efficient way to find a repeated number in a sorted array? My answer was based on using a hash table with key as array element and

In my application, I let users choose how they want their data arranged (sorted or unsorted). When they choose sorted, I simply sort everything. That part is straightforward to me. However, when they

What does indexpath mean for UICollectionView's supplementaryView or decorationView? layoutAttributesForSupplementaryViewOfKind:atIndexPath: layoutAttributesForDecorationViewOfKind:atIndexPath:

http://jsfiddle.net/dPwQA/2/ says I push a new item into an array, after sorted, I want to get its index. function sortInt(a, b) { return a - b; } numbers = [7,6]; numbers.sort(sortInt); $('#text').te

I have to make a program in Java to create a sorted array in descending order. When i try to insert 10, 3 it is ok, but when i insert 10, 3, 8 the result is 10, 8, 0. For some reason the 0 appears on

I'm trying to determine the indexPath of the select row of my UITableView so I can pass the relevant data accordingly. Right now it keeps logging 0 no matter what cell I select. Any ideas why? (Yes, t

This could be a possible duplicate question but still asking. I have a list of date (dates are in unsorted) 1/5/2012,1/10/2012, 1/1/2012, 1/7/2012 (MM/dd/yyyy)( only date no timestamp) First I sorted

I would like to calculate the int value of a IndexPath that has rows and sections to then use to select a object from a NSArray using ObjectAtIndex. I only have a string value from the UITable so I am

You have an ascending list of numbers, what is the most efficient algorithm you can think of to get the ascending list of sums of every two numbers in that list. Duplicates in the resulting list are i

I´m searching for a sorted Array or Map (Dict...whatever) in Objective-C on iOS. Is there something comparable (sorting when inserting) or do I have to override the getter/setter and sort the data str

This is an interview question: given an integer x and a sorted array a[] of N distinct integers, design a linear-time algorithm to determine if there exists two distinct indices i and j such that a[i]

I am trying to optimize a search through a very short sorted array of doubles to locate a bucket a given value belongs to. Assuming the size of the array is 8 doubles, I have come up with the followin

i use this code to get info about friends <?php $fql = SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ; $fUIDS = $facebook->api(a

I am able to get the text value of a cell like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger *row = [indexPath row]; // NOW GET THE NAM

I need to estimate if the array list is sorted (don't sort). When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first And return