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 have a sorted array ( in ascending order) and I want to find the subscript of a number in the array which is the the next highest number to a given number. For example,if I have 67 as a given number

A question asked in Oracle interview find missing element in a sorted array of n element ranges 1 to n without accessing full array. I am wondering if there is a way to find missing element without

I want to create an almost sorted array, as efficiently as possible (to explore effect of random subtle variations in a list, I expect to call this routine a lot). I have Ruby code which does what I

I have an array with numbers like this: 25, 19, 14, 11, 9, 7, 5, 2 and I want to insert a number at the right position I wrote this but I don't know if the code is true. Can you help me? if(number &g

How to deselect previously selected row in tableView ?? I can't find solution to obtain oldIndexPath -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITabl

I have a UITableView with multiple sections. Instead of the typical approach of using an array per section, I am using one array. Anyway, I am having trouble getting the current indexPath.row as if th

Given a sorted array of integers, can we build a sorted array of the sums of all pairs in O(n^2)? A trivial solution would be to build the array of sums in O(n^2) and then to sort it in O(n^2 (log(n^2

For some reason, I have a sorted php array: $arr_questions = Array [6] 0 Array [6] 1 Array [6] 2 Array [6] 3 Array [6] 4 Array [6] 5 Array [6] each of the positions is another array. This time it i

Skiena, in The Algorithm Design Manual, states that insertion into a sorted array is O(n). Yet searching for an item in a sorted array is O(log n), because you can do a binary search. Couldn't inserti

I'm working on a homework problem and I'm having some difficulties creating a O(n*logn) solution. I need to write a function that takes a pre-sorted array and a value to search for. I then need to fin

so how to make such logic int[] arr = {2, 5, 3}; if (/* arr is sorted */) .... else ... Its bad that method Array.sort is void

how to get the sorted NSArray from the NSArray that contains number string with two decimal points. Can anyone help me in this regard. Thanks in advance.

i have an array like this: 1,2,3,5,6,4 it is 99% sorted and has 40K elements. i can put them in an array, list, linked list, ... but i don`t know the fastest way to sort them!

First let me say that I am new to C so my approach is basic. I am attempting to check a sorted array for a point where it was rotated. For example (1 2 4 5 9) becomes (5 9 1 2 4). I am attempting to

What is an easy way in Java to sort the array of elements maintaining information about indexes where the elements were stored in the original array? Is there a built-in function? There are few ways I

i've a table --------- | NAME | |---------| | arun | | balu | | sunny | | binu | | binoy | | cinny | | aiju | | dolly | --------- i've wrote a query to get sorted list in mysql SELECT name FROM stu

I want to convert a sorted integer array into a binary search tree. I believe I understand how to do this. I have posted my pseudo-code below. What I cannot picture is how the recursion actually works

I have created an tableview with an array. There are 7 items in my array. But in cellForRowAtIndexPath method i'm unable to get indexpath.row == 6. What could be the reason. Code is below for this.

I want a sorted list of files from a directory. How do I apply the sort function to a list with IO monad? import System.Directory import Data.List sortedFiles :: FilePath -> IO [FilePath] sortedFil

I'd like my array to be sorted by unique day and only show the day value once with all the times for that day included. Desired Example: Informal Monday: 2PM to 3PM, 3PM to 4PM, 4PM to 5PM Tuesday: 2

Here is a code pasted from here I'm unable to understand what it is doing. I could think of a simple algorithm to find median of two sorted arrays when merged. The idea is to compare the medians (m1,

I don't get it. The cellForRowAtIndexPath function's indexPath parameter is always 0. I currently have 7 rows in my table. What my table shows is 7x the first table row. What could cause the indexPath

I made custom cell with scrollViewon right side of the cell and imageView on left side. Now when I click on imageView I get correct value for indexPath.row, but when I click on scrollView indexPath.ro

For this problem I have a sorted Array of doubles. I need to be able to quickly and efficiently find index of the value that is closest to, but not over, n. Efficiency is key, the assignment states is

Is there any way to maintain a sorted array of objects? For example, if I have an object with properties ID, Date, Name and a collection of these objects: $col = array(); public function addNewObject(

How do I, from a cell, get its indexPath in a UITableView? I've searched around stack overflow and google, but all the information is on the other way around. Is there some way to access the superView

We have given an array of n integers of which first n-(squareroot)n elements are sorted (it means (root)n elements from the last are not sorted). We have to sort the entire array with minimum time com

I got a call from Amazon to attend one of their interview and given a test to write most efficient solution to the below problem. To find smallest position of 1 in sorted array of 0 and 1. e.g. in arr

Im trying to store the indexPath.row value but on the next view Im getting 0. In selectedRecipiesViewController I always get a zero. Here is my code. MainRecipieViewController.h @property (assign) i

How do i use indexPath.row to get an object from an array? I have tried with the following code but it returns signal SIGABRT.. Please help - (UITableViewCell *)tableView:(UITableView *)tableView ce

i wrote a little algoritm for marge to sorted array. but i have porblem with that. #include <iostream> using namespace std; int main() { // main function started form here: int firstArray[10] =

So I have a detail view that need to display information by getting a index integer caculated from the indexpath from the selected cell in self.tableview, I've been using the NSFetchedResultsControlle

I'm new here and new to Javascript and web programming (I actually know a little bit of Java not for web). So here's the problem I have an array of strings (name of Albums to be more specific) and I a

I have two textfields in a custom cell how to get the indexpath value of Tableview cell in textfield delegate methods I want to get the input value from user and save it to the relavent object. the us

I need to read sorted array from input to awk/gawk and get median. I don't want to store the whole array and am trying to get constant space for the calculation. Are you aware of any algorithm doing t

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

I am using the webView in customcell of tableview, webView covers total area of the customcell. When i click the web view in custom cell i can't get the indexpath. I have done the basic coding such as

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

Possible Duplicate: Interview Q: sorting an almost sorted array (elements misplaced by no more than k) I have a partially sorted array with the property that every element is within d units of its p

I have an unsorted array and need to extract the longest sequence of sorted elements. For instance A = 2,4,1,7,4,5,0,8,65,4,2,34 here 0,8,65 is my target sequence I need to keep track of the index whe

What's the best way to get the sum of SCORES in a Redis sorted set?

I need to create a method that inserts an integer into a sorted array of integers. I had already done this method but not with integers but with strings. Now I have a problem with integers. My method

We have an n x m matrix whose rows are sorted, we need to print the numbers in the matrix in ascending order. The columns are not necessarly sorted. The solution that came to my mind was simple mergin

How to get a list of items, that is in sorted gridControl? I need to create a new List with only values, sorted by GridControl.

I have an indexPath.row that is 1 and logs 1(when using NSLog). If I call indexPath.row-1 (should return 0) it returns 4294967295. I'm trying to return an objectAtIndex:indexPath.row-1 but that's whe

Say I have a dictionary with keys (words) and values (scores) as follows: GOD 8 DONG 16 DOG 8 XI 21 I would like to create an NSArray of dictionary keys (words) that is sorted first by score then alp

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 having two sorted int Arrays (both one-dimensional, Array1 could have up to several million entries, Array2 will probably be a lot smaller, I am thinking six figures, no duplicates) where I have

My teacher gave me the next task: On a sorted array, find the number of occurrences of a number. The complexity of the algorithm must be as small as possible. This is what I have thought of: public s

Can I do a foreach on a function that takes an array object and sorts it. I know I can do a dependent observable, but I have an array of an array coming from the mapping plugin. foreach: sortList($arr