+ Start a Discussion
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN 

Need to sort all the columns in excel sheet while downloading from vfpage

Hi All,

I am downloading an excel report from VFpage i am having four columns header1, header2, header3, header4, i am having sample data as below. 

header1 header2 header3 header4
aaaa     aaaaaa  aaaaaaa aaaaaa
bbbb     bbbbbb  bbbbbbb bbbbbb
aaaa     aaaaaa  aaaaaaa aaaaaa
aaaa     aaaaaa  aaaaaab aaaaab
aaaa     aaaaaa  aaaaaab aaaaaa
aaaa     aaaaab  aaaaaab aaaaab

I need to sort as below. Please how can we achieve this. Please note that i am passing a map whcih holds the data. here is the format. 
map<header1,List < obj>> where obj contains header2, header3, header4. 

header1 header2 header3 header4
aaaa     aaaaaa  aaaaaaa aaaaaa
aaaa     aaaaaa  aaaaaaa aaaaaa
aaaa     aaaaaa  aaaaaab aaaaaa
aaaa     aaaaaa  aaaaaab aaaaab
aaaa     aaaaab  aaaaaab aaaaab
bbbb     bbbbbb  bbbbbbb bbbbbb

Please help me how to acheive this. Thanks
 
Best Answer chosen by SHAIK MOHAMMAD YASEEN
Malni Chandrasekaran 2Malni Chandrasekaran 2
Shaik,
Can you try recursive Map collection to hold your data?
Something like,
Map<header1, Map<header2, map<header3, list<header4>>>>

I have use 2 level of recursive map collection.
When I get time, I can try this  and confirm you back. Meanwhile, if you can, please try if your logic permits.

All Answers

Malni Chandrasekaran 2Malni Chandrasekaran 2
Hi Shaik,
I am just trying to understand, you are retrieving the data in a map collection where Header1 column is the Key data and other 3 columns are values in the form of an object.
If my understanding is correct, you can not have duplicate data in Header1 - Key data should be unique in Map collection.

If you have to sort the key data (header1),
 - move the key data to the list and sort 
eg
list<string> sortList = new List<String>();
sortList.addAll(Map.keySet());
sortList.sort();                            //Header 1 will be sorted
for(String a: sortList)
{
  System.debug( 'Header 1 ' + a + 'Other Headers' + Map.get(a)) ;
}

Thanks
SHAIK MOHAMMAD YASEENSHAIK MOHAMMAD YASEEN
you are right Malni Chandrasekaran in map key cannot be duplicated but in my requirement the map structure is like this map < string , List < obj >> which means a key can have multiple records so it will be repeated for all the list elements,

Actually i am able to sort hader1 and header2, but unable to sort header3, header4, can you please help me with that
Malni Chandrasekaran 2Malni Chandrasekaran 2
Shaik,
Can you try recursive Map collection to hold your data?
Something like,
Map<header1, Map<header2, map<header3, list<header4>>>>

I have use 2 level of recursive map collection.
When I get time, I can try this  and confirm you back. Meanwhile, if you can, please try if your logic permits.
This was selected as the best answer