5

I have unsorted dictionary array and I want to sort it according to artist names.Unsorted list are given below:

            {
                artist = "Afro Latin Jazz Orchestra";
                id = 10;
            },
                    {
                artist = "Avey, Bobby";
                id = 17;
            },
                    {
                artist = "American Symphony Orchestra";
                id = 32;
            },
                    {
                artist = "Akpan, Uwem";
                id = 97;
            },
                    {
                artist = "Austin, Ivy ";
                id = 123;
            },
                    {
                artist = "American Theatre Orchestra";
                id = 153;
            },
                    {
                artist = AudraRox;
                id = 171;
            },
                    {
                artist = "Atlas, James";
                id = 224;
            },
                    {
                artist = "Alden, Howard";
                id = 270;
            },
                    {
                artist = Astrograss;
                id = 307;
            },
                    {
                artist = "Adan, Victor";
                id = 315;
            },
                    {
                artist = "Alegria, Gabriel";
                id = 316;
            },
                    {
                artist = "Andersen, Kurt";
                id = 412;
            },
                    {
                artist = Antares;
                id = 420;
            },
                    {
                artist = "Austen, Jane ";
                id = 426;
            },
                    {
                artist = "Acuna, Claudia";
                id = 443;
            },
                    {
                artist = "Akinmusire, Ambrose";
                id = 444;
            },
                    {
                artist = "Anderson, Laurie Halse";
                id = 559;
            },
                    {
                artist = "Alvarez, Javier";
                id = 591;
            },
                    {
                artist = "Alexander, Jane";
                id = 674;
            },
                    {
                artist = "Andy Teirstein";
                id = 695;
            },
                    {
                artist = "Afro-Cuban Jazz Saxtet";
                id = 707;
            },
                    {
                artist = "Aurora ";
                id = 708;
            },
                    {
                artist = "Aurora ";
                id = 709;
            },
                    {
                artist = "August, Gregg ";
                id = 715;
            },
                    {
                artist = "Aldous, Brian";
                id = 777;
            },
                    {
                artist = "Anne Enright";
                id = 1130;
            }

And after sorting using descriptor

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"artist" ascending:YES];
 NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
 sortedArray = [artistArray sortedArrayUsingDescriptors:sortDescriptors];

it gives sorted array as

        {
            artist = "Acuna, Claudia";
            id = 443;
        },
            {
            artist = "Adan, Victor";
            id = 315;
        },
            {
            artist = "Afro Latin Jazz Orchestra";
            id = 10;
        },
            {
            artist = "Afro-Cuban Jazz Saxtet";
            id = 707;
        },
            {
            artist = "Akinmusire, Ambrose";
            id = 444;
        },
            {
            artist = "Akpan, Uwem";
            id = 97;
        },
            {
            artist = "Alden, Howard";
            id = 270;
        },
            {
            artist = "Aldous, Brian";
            id = 777;
        },
            {
            artist = "Alegria, Gabriel";
            id = 316;
        },
            {
            artist = "Alexander, Jane";
            id = 674;
        },
            {
            artist = "Alvarez, Javier";
            id = 591;
        },
            {
            artist = "American Symphony Orchestra";
            id = 32;
        },
            {
            artist = "American Theatre Orchestra";
            id = 153;
        },
            {
            artist = "Andersen, Kurt";
            id = 412;
        },
            {
            artist = "Anderson, Laurie Halse";
            id = 559;
        },
            {
            artist = "Andy Teirstein";
            id = 695;
        },
            {
            artist = "Anne Enright";
            id = 1130;
        },
            {
            artist = Antares;
            id = 420;
        },
            {
            artist = Astrograss;
            id = 307;
        },
            {
            artist = "Atlas, James";
            id = 224;
        },
            {
            artist = AudraRox;
            id = 171;
        },
            {
            artist = "August, Gregg ";
            id = 715;
        },
            {
            artist = "Aurora ";
            id = 708;
        },
            {
            artist = "Aurora ";
            id = 709;
        },
            {
            artist = "Austen, Jane ";
            id = 426;
        },
            {
            artist = "Austin, Ivy ";
            id = 123;
        },
            {
            artist = "Avey, Bobby";
            id = 17;
        }

But still it is not sorted completely . Any idea how can I sort it alphabetically using artist name.Help me please!

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
  • 5
    What is the problem with the sorting? The results you show are in order? What order do you expect them to be in? – David Rönnqvist Apr 10 '12 at 06:21
  • possible duplicate of [How can I sort an NSArray containing NSDictionaries?](http://stackoverflow.com/questions/3361207/how-can-i-sort-an-nsarray-containing-nsdictionaries) – jscs Apr 10 '12 at 06:22
  • Is your specific problem that the sort is case-sensitive? If so, you want `[NSSortDescriptor sortDescriptorWithKey:@"artist" ascending:YES selector:@selector(caseInsensitiveCompare:)]`. – Cowirrie Apr 10 '12 at 06:22
  • Just follow [this link][1] you can sort it by using sort descriptor. [1]: http://stackoverflow.com/questions/3925666/sorting-an-nsarray-of-nsdictionary – Nilesh Kikani Apr 10 '12 at 06:22
  • 1
    it IS sorted alphabetically by the `artist`. [Here](http://en.wikipedia.org/wiki/English_alphabet) is the english alphabet if you need it to check – Novarg Apr 10 '12 at 06:56
  • @Prince kumar please write down what order you need in output. – priyanka Apr 10 '12 at 09:47

1 Answers1

-1

Using for loop set keys for every dictionaries values like this

for(int i =0;i<[Arr count];i++)
{
   [dict setValue:[Arr objectAtIndex:i] forKey:[[Arr objectAtIndex:i] valueForKey:artist]];
}

now you'll get like this

Acuna, Claudia = {
    artist = "Acuna, Claudia";
    id = 443;
}       
Adan, Victor =  {
    artist = "Adan, Victor";
    id = 315;
}       
Afro Latin Jazz Orchestra =   {
    artist = "Afro Latin Jazz Orchestra";
    id = 10;
}       
Afro-Cuban Jazz Saxtet   {
    artist = "Afro-Cuban Jazz Saxtet";
    id = 707;
}       
Akinmusire, Ambrose =   {
    artist = "Akinmusire, Ambrose";
    id = 444;
}       

NSArray *KeyArr = [dict allKeys];

[KeyArr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

Now you'll get sorted keys in array, Using this array you can get sorted dictionart by using sorted keys in alphapet

for(int i= 0;i<[dict count];i++)
{
     NSLog("Test val:%@",[dict valueForKay:[keyArr objectAtIndex:i]]);
}

Finally you'll get what you are seeking now.. enjoy this coding..
If you have any doubt feel free to ask..

tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
Prakash
  • 812
  • 6
  • 16
  • This won't actually work, because the sorted version of `KeyArr` is discarded. You could store it using `KeyArr = [KeyArr sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];`. However, this would be less efficient than sorting the existing array using the "artist" key and the `caseInsensitiveCompare:` selector, if that is what the OP needs. – Cowirrie Apr 10 '12 at 06:48