none
How to Sort HealthRecordItemCollection

    Question

  • Here I want sort HealthRecordItemCollection items in ascending order so following code I am develop. Could somebody help me for ascending code for that. 

    OfflineWebApplicationConnection offlineConn = new OfflineWebApplicationConnection(PersonID);
                    offlineConn.Authenticate();

                    PersonInfo obj = offlineConn.GetPersonInfo();
                    HealthRecordAccessor accessor = new HealthRecordAccessor(offlineConn, RecordID);

                    HealthRecordSearcher searcher = obj.SelectedRecord.CreateSearcher();
                    HealthRecordFilter filter = new HealthRecordFilter(HeathDataTypeID);
                    //HealthRecordFilter filter = new HealthRecordFilter(ExerciseSamples.TypeId);

                    //filter.CreatedDateMin = Start_Datetime;
                    //filter.CreatedDateMax = End_Datetime;

                    filter.EffectiveDateMin = Start_Datetime;
                    filter.EffectiveDateMax = End_Datetime;

                    searcher.Filters.Add(filter);

                    HealthRecordItemCollection items = searcher.GetMatchingItems()[0];

    thanks,
    Thursday, May 06, 2010 1:46 PM

Answers

  • Hello Ganesh,

    Please use the below sample code for sorting HealthRecordItemCollection. For sorting a collection you need to create a Comparer class(HeightComparer) as mentioned below.


    //This code you can put after your code

      HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
      items.Sort(new HeightComparer());
      foreach(HealthRecordItem item in items)
      Response.Write(item.ToString());      

    //Create a new class and it should exetend Icomparer     

      public class HeightComparer : IComparer<HealthRecordItem>
        {

            #region IComparer<HealthRecordItem> Members

            public int Compare(HealthRecordItem x, HealthRecordItem y)
            {

                Height height1 = x as Height;

                Height height2 = y as Height;

                if ((height1 != null) && (height2 != null))
                {

                    return height1.Value.Value.CompareTo(height2.Value.Value);

                }

                else
                {

                    return -1;
                }

            }

            #endregion

        }

    Please use the Msdn reference for sort method http://msdn.microsoft.com/en-us/library/ff357459.aspx

    Hope this helps.

    Regards,

    Madan Kamuju

    Thursday, May 06, 2010 8:30 PM
  • Hi Ganesh,

    For sorting all items against date means(when value,ascending order), You need to change the if condition in HeightComparer class as below.

    if ((height1 != null) && (height2 != null))

    {

    return height1.When.CompareTo(height2.When);

    }

    Please let me know if you need any thing.

    Thanks and Regards,

    Madan Kamuju

     

     

    Friday, May 07, 2010 4:04 PM
  • Hello Ganesh,

    If you would like to implement the above function as generalized function, Then you need to use some swith case statements. You can pass the dataType name as string to the constructor of the comparer class and then handle it in the switch case, because the return values may differ from datatype to datatype as per requirement.

    Please use the below sample code:

    //Constructor
          string strDataType = string.Empty;

          public HeightComparer(string dataType)
            {
                strDataType = dataType;
            }

        
         //Compare method

            public int Compare(HealthRecordItem x, HealthRecordItem y)
            {
              
                switch (strDataType)
                {
                    case "Height" :
                       if ((x != null) && (y != null))
                        {
                            return (x as Height).When.CompareTo((y as Height).When);
                        }
                        else
                        {
                            return -1;
                        }                 
                   
                    case "Weight" :
                       
                        if ((x != null) && (y != null))
                        {
                            return (x as Weight).Value.Value.CompareTo((y as Weight).Value.Value);
                        }

                        else
                        {
                            return -1;
                        }
                       
                    case "default" :
                        return -1;
                }
                return -1;

              }

    Regards,

    Madan Kamuju

    Wednesday, May 12, 2010 6:30 PM

All replies

  • Hello Ganesh,

    Please use the below sample code for sorting HealthRecordItemCollection. For sorting a collection you need to create a Comparer class(HeightComparer) as mentioned below.


    //This code you can put after your code

      HealthRecordItemCollection items = searcher.GetMatchingItems()[0];
      items.Sort(new HeightComparer());
      foreach(HealthRecordItem item in items)
      Response.Write(item.ToString());      

    //Create a new class and it should exetend Icomparer     

      public class HeightComparer : IComparer<HealthRecordItem>
        {

            #region IComparer<HealthRecordItem> Members

            public int Compare(HealthRecordItem x, HealthRecordItem y)
            {

                Height height1 = x as Height;

                Height height2 = y as Height;

                if ((height1 != null) && (height2 != null))
                {

                    return height1.Value.Value.CompareTo(height2.Value.Value);

                }

                else
                {

                    return -1;
                }

            }

            #endregion

        }

    Please use the Msdn reference for sort method http://msdn.microsoft.com/en-us/library/ff357459.aspx

    Hope this helps.

    Regards,

    Madan Kamuju

    Thursday, May 06, 2010 8:30 PM
  • Thanks,

    But here I have to sort all items against date means(when value,ascending order)

    so please could you help me for that functionality.

     

     

    Friday, May 07, 2010 7:35 AM
  • Hi Ganesh,

    For sorting all items against date means(when value,ascending order), You need to change the if condition in HeightComparer class as below.

    if ((height1 != null) && (height2 != null))

    {

    return height1.When.CompareTo(height2.When);

    }

    Please let me know if you need any thing.

    Thanks and Regards,

    Madan Kamuju

     

     

    Friday, May 07, 2010 4:04 PM
  • Thanks Madan,

    Now I got sorted records against date-time but now how can i make this function generalize(means common for all health-data type).Because i have Exercise,Blood pressure records and I want to sort this data type records,Using same function 

     

    please help me.

    Wednesday, May 12, 2010 7:26 AM
  • Hello Ganesh,

    If you would like to implement the above function as generalized function, Then you need to use some swith case statements. You can pass the dataType name as string to the constructor of the comparer class and then handle it in the switch case, because the return values may differ from datatype to datatype as per requirement.

    Please use the below sample code:

    //Constructor
          string strDataType = string.Empty;

          public HeightComparer(string dataType)
            {
                strDataType = dataType;
            }

        
         //Compare method

            public int Compare(HealthRecordItem x, HealthRecordItem y)
            {
              
                switch (strDataType)
                {
                    case "Height" :
                       if ((x != null) && (y != null))
                        {
                            return (x as Height).When.CompareTo((y as Height).When);
                        }
                        else
                        {
                            return -1;
                        }                 
                   
                    case "Weight" :
                       
                        if ((x != null) && (y != null))
                        {
                            return (x as Weight).Value.Value.CompareTo((y as Weight).Value.Value);
                        }

                        else
                        {
                            return -1;
                        }
                       
                    case "default" :
                        return -1;
                }
                return -1;

              }

    Regards,

    Madan Kamuju

    Wednesday, May 12, 2010 6:30 PM
  • thanks madan.
    Thursday, May 13, 2010 12:29 PM
  • Hi Madan,

    The solution you have given, works after the items are retrieved. In my case, we have to support pagination.

    Is there a way we can create a searcher with the sorting parameters(datetime, name), which will return the items in sorted order.

    Tuesday, July 16, 2013 8:45 AM
  • Any update on this?

    Hi Madan,

    The solution you have given, works after the items are retrieved. In my case, we have to support pagination.

    Is there a way we can create a searcher with the sorting parameters(datetime, name), which will return the items in sorted order.

    Tuesday, August 06, 2013 11:17 AM