1

I have an array that contains file names as:

NSArray *filename = @[@"anoop", @"anoop100", @"anoop5"];

Now if user creates a new file the new name should be anoop101.

What have I tried:

I sorted the array in that case anoop5 goes at last and incrementing the extracted number gives anoop6, this is not my use case.

I need to sort the strings on the integer part so that anoop100 goes to the end of the list.

Any help?

Edit:

Someone marked it as duplicate of numeric search. In fact its not only numeric search, it has string search as well. So alone makes and incorrect answer and pity for the closer.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 1- all the strings that do not have Numerical value should be added in start. 2- break the string from Numerical part. 3- sort the numerical values array. 4- start matching and adding the values from the origional array to the resultant array. – Saad Chaudhry Jun 20 '16 at 08:33
  • Use `sortedArrayUsingComparator:` and `compare:options:` with `NSNumericSearch` for the correct sort. Then get last object, and you can use http://stackoverflow.com/questions/4663438/objective-c-find-numbers-in-string or substring if you know the "base" (in your case "anoop") – Larme Jun 20 '16 at 08:35
  • filename =[filename sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; will sort finename array as per your requirement – Muhammad Adnan Jun 20 '16 at 08:40
  • @Droppy: How you came to the fact as NumericSearch will do the work? If that was the case I wouldn't have asked the question! Its bit more that that. – Anoop Vaidya Jun 20 '16 at 11:43
  • It will correctly sort your array even though the strings contain non-numeric characters. – Droppy Jun 20 '16 at 12:20

2 Answers2

2
filename =[filename sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
Muhammad Adnan
  • 2,668
  • 15
  • 27
  • you can now get last object . replace anoop string with empty string , then get integer value from string. add new fileName as stringformat anoop[integervalue+1]. You would need to add new object in NSMutablearay and then reassign that mutable array as [NSArray arrayWitharray :newmutablearray] to origional array – Muhammad Adnan Jun 20 '16 at 08:49
  • @AnoopVaidya Have you tried it? See [this answer](http://stackoverflow.com/questions/30215508/nssortdescriptor-evaluating-ascending-numbers-swift). – Droppy Jun 20 '16 at 12:27
  • @Droppy: My bad.... How can I missed this, I closed this quesation my self. I cant delete it coz it contains answers. Thanks a lot to both of you & Md. Adnan. – Anoop Vaidya Jun 20 '16 at 12:35
0

You dont' need to sort

Suppose you don't have any file in your array then it will looks like

NSArray *filename = @[]

now you have to add new file it should be anoop

check the array count and it is 0 so file name should be anoop0 or **anoop*

and add to array

NSArray *filename = @[@"anoop"]

now you can check count it is 1 so next file would be anoop1 and so on

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98