function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

Help with idea for how to sort response to post request

Hello, I am making a post request, which returns results that look like this: 
Hospital from date 01:07/25/2020;
Hospital to date 01:08/04/2020;
Hospital NPI 01:1245678;
Hospital from date 02:06/29/2020;
Hospital to date 02:07/16/2020;
Hospital NPI 02:1245678;
Hospital from date 03:06/04/2020;
Hospital to date 03:06/09/2020;
Hospital NPI 03:1245678;
SNF from date 01:07/16/2020;
SNF to date 01:07/25/2020;
SNF NPI 01:1245678;
SNF from date 02:06/09/2020;
SNF to date 02:06/28/2020;
SNF NPI 02:1245678;
SNF from date 03:06/01/2020;
SNF to date 03:06/04/2020;
SNF NPI 03:1245678;
SNF from date 04:05/29/2020;
SNF to date 04:05/31/2020;
SNF NPI 04:1245678;


The way this works is that each 3 lines are one "group". So Hospital from date01, hospital to date01, and hospital NPI01 all go together, and same with the set with 02, and the SNF as well. What I need to do is sort of of those "groups" in order of the "from date". Meaning, I need to take all of the "From Dates" from all of the above lines (Hospital and SNF all together), and sort them in order of dates. 

I know that I can add all of the from dates to a list and sort that list. But where I am getting stuck is how do I keep the "to date" and the "NPI" together with the from date after I sort it. 

Would anyone have an idea for how i can accomplish this?
Vishwajeet kumarVishwajeet kumar
Hello,
Not sure if you have tried to make composite key like :   "date" - "NPI", put in list, sort it as List of string. After sorting you can split the date, NPI data and use NPI to organize the records.

Other way could be to use algorithms like Bubble Sort, by implementing it for this data structure.

Thanks
Michael MMichael M
Hi Vishwajeet, I'm not familiar with either of the above- i am a bit new to programming. Would you be able to show me a basic exmaple of what it would look like?
Vishwajeet kumarVishwajeet kumar
Hello,
np, let me try.
For the first one - string sorting, we can start with creating a wrapper class to store data.

Public Class DataWrapper{
String NPI;
String fromDate;
String toDate;
}

Store all data in a Map with NPI as key.
Map<String, DataWrapper> dataMap = new Map<String,DataWrapper>();
Sample Map Data: {'01:1245678' => new DataWrapper()}

List<String> compositeKeyList = new List<String>();      //Contains data which will be in sortable format.
Composite key for each DataWrapper would be like :  07/25/2020-01:1245678     (Remove 01 from fromDate)
add all dataMap DataWrapper composite key to compositeKeyList by looping through.
After adding some data list would look like : {'07/25/2020-01:1245678','06/29/2020-02:1245678'','07/16/2020-03:1245678'}
compositeKeyList.sort();           //Sort the list

Loop through the compositeKeyList and arrange the DataWrapper data for use :
List<DataWrapper> sortedData = new List<DataWrapper>();                    //Final data which is sorted by fromDate

for(String v_CompKey : compositeKeyList){
  String v_NPI = v_CompKey.split('-')[1];                //get 2nd value, which is NPI
 sortedData.add(dataMap.get(v_NPI ));                 //get data from dataMap using NPI and add it to list
}

Try and see.

If it doesn't work bubble sorting algorithm (https://www.geeksforgeeks.org/bubble-sort/) can be implmented on DataWrapper, it will only change the implementation to use custom method to sort instead of using sort(); 

Thanks