I received a requirement and solution from a reader named Khadar few days ago. The requirement was to sort a property set based on certain element in property set. I tested the solution provided by him but it was not working. As I found the requirement interesting so I went ahead coded the solution myself
Requirement: To sort data in a property set based on an element in the property set. In the example below let’s assume that we have a property set as described in the table below. Each child in the property set has 4 properties mentioned in the table and we would like to sort it according to the date.
|
Row Id |
Date |
Status |
Comments |
|
1 |
08/08/2011 |
Open |
Test |
|
2 |
07/07/2011 |
In Process |
Test2 |
|
3 |
09/09/2011 |
Closed |
Test3 |
Solution: My solution takes two input arguments a property set to be sorted and type of sorting. It returns the sorted property set back
function SortPS(Inp,type)
{
var inChildCount = Inp.GetChildCount();//get the child count
var Outputs = TheApplication().NewPropertySet();
var i=0, j=0, outChildCount = 0;
for(i=0;i<inChildCount;i++)
{
if(i==0) //add the first element as it is
Outputs.AddChild(Inp.GetChild(i));
else
{
outChildCount = Outputs.GetChildCount();
for(j=0;j<outChildCount;j++)
{
//if descending then add bigger element before
if(Date.parse(Inp.GetChild(i).GetProperty(“Date”)) > Date.parse(Outputs.GetChild(j).GetProperty(“Date”)) && type==”D”)
{
if(j==0){ Outputs.InsertChildAt(Inp.GetChild(i),j); break; }
else { Outputs.InsertChildAt(Inp.GetChild(i),j-1); break;}
}
//if ascending then add smaller element before
else if(Date.parse(Inp.GetChild(i).GetProperty(“Date”)) < Date.parse(Outputs.GetChild(j).GetProperty(“Date”)) && type==”A”)
{
if(j==0){ Outputs.InsertChildAt(Inp.GetChild(i),j); break; }
else { Outputs.InsertChildAt(Inp.GetChild(i),j-1); break;}
}
// if equal then add it right in this position
else if(Date.parse(Inp.GetChild(i).GetProperty(“Date”)) == Date.parse(Outputs.GetChild(j).GetProperty(“Date”)))
{
Outputs.InsertChildAt(Inp.GetChild(i),j);
break;
}
}
//if not inserted yet then insert it in last
if(j==outChildCount)
Outputs.AddChild(Inp.GetChild(i));
}
}
return(Outputs);
}
Few Remarks:
- I have not included error handling in the example code. Remember to add it before using the code in production.
- Don’t forget to nullify the objects in finally block
- This is not a generalized solution but it shouldn’t be difficult to change it to sort on any type of element.
I know this might not be the most efficient way to sort the property set. If you think you know a better way to do it then please use contribute page to send you solution or use comments to provide your feedback. Any contribution on your part will help Siebel community as a whole.




