I am developing an iPhone application in objectiveC in which I have one array having alphanumeric, multilingual, special character data as shown below:
(
{
name = “#”;
},
{
name = “iPad”;
},
{
name = “عينة”;
},
{
name = “1”;
},
{
name = “أ”;
},
{
name = “10”;
},
)
Sorting should provide me result as below,
(
{
name = “iPad”;
},
{
name = “أ”;
},
{
name = “عينة”;
}
{
name = “#”;
},
{
name = “1”;
},
{
name = “10”;
},
)
Means first English and Arabic should be in alphabetical ascending order then special characters and then numbers in ascending order.
There could be some improvement, by this is what you can do with
sortedArrayUsingComparator:
and a custom comparator block.Main idea:
- Get first char of the string.
- Determine what its "kind" (Letter, Arabic, Number, Other)
- Sort accordingly.
I started with these "kind":
And use this method to get the CharacterOfKind:
I don't speak Arabic, and I never had to code with Arabic text. So I took these info of "character range" from the net. I read something on Objective-C here on StackOverflow, but I couldn't retrieve it. Maybe my memory is playing with me, but the range may have changed (more ranges added). I find the method a little ugly, but that's to get the main point.
Output:
I got the solution using regular expression as below,
This way I managed to get all arrays using their relevant regular expression.