0

Following is the array I am receiving in response.
 I have to show this data on UITableView where header title will be my transactionDate and items related to that date will go as number of rows in that section.

For below code, I have successfully pulled out unique dates from given list and I got 3 elements in unique date array.

Now I want another array which will have items associated to the date from unique date array.

So in result I should get 3 arrays for transactionDate value. It should be like array1 with 2 value objects related to date 01/02/2021 array2 will be with 1 value related to date 27/01/2021 and array3 will be with 1 value object related to value to date 25/01/2021

This data will go on increasing as transactions for given date will go on, so some dynamic implementation is needed.

My all data elements are in array activityListArray which contains all these elements. Among which I have filtered out unique dates as below,

for i in activityListArray{
            date_array.append(i.transactionDate!)
        }
        let unique = Array(Set(date_array))

Now to get expected result what should I do next….?

Thanks in advance.

Response I am getting is below,

 {
            accountNumber = 0000078;
            amount = 0;
            buyingAmount = "277.42";
            buyingCurrency = EUR;
            currency = "";
            customerInstruction = "0201000009958346-000000218";
            payeeName = "Ash Roy";
            reasonCode = "";
            sellingAmount = "254.00";
            sellingCurrency = GBP;
            status = AWAITINGBANKAPPROVAL;
            subType = "Payment without Fx";
            transactionDate = "01/02/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000210";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "27/01/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000207";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "25/01/2021";
            type = "";
        },
                {
            amount = 0;
            buyingAmount = "436.39";
            buyingCurrency = GBP;
            currency = "";
            customerInstruction = "0201000009958346-000000206";
            reasonCode = "";
            sellingAmount = "788.00";
            sellingCurrency = CAD;
            status = PENDING;
            subType = "Payment without Fx";
            transactionDate = "01/02/2021";
            type = "";
        }
Sagar
  • 1,286
  • 3
  • 19
  • 34

1 Answers1

1

You dont need to pull out unique dates, Swift standard library has already provided an API to Dictionary, where in you can group the collection of objects based on specific field using init(grouping:by:)

read all about it here https://developer.apple.com/documentation/swift/dictionary/3127163-init

let dict = Dictionary<String,[YourObject]>(grouping: activityListArray, by: {$0.transactionDate})

BTW Dictionary<String,[YourObject]> explicit type declaration is not needed, just realised it, so it can be as simple as

let dict = Dictionary(grouping: activityListArray, by: {$0.transactionDate})

This will return a Dictionary with unique transactionDate as keys and array of objects with that transactionDate as values.

Now you can simply return number of sections as dict.keys.count and number of rows in each section as dict[Array(dict.keys)[indexPath.section]]?.count

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • @sagar: Please check the answer posted and lemme know if you are still facing issue, if its working fine please consider accepting the answer – Sandeep Bhandari Feb 17 '21 at 17:16
  • 1
    @ Sandeep Bhandari :- Thanks buddy, that helped me a lot and kudos to answer. Thank you very much. – Sagar Feb 18 '21 at 06:34
  • 1
    @sagar: glad I could help :) – Sandeep Bhandari Feb 18 '21 at 07:47
  • @ Sandeep Bhandari:- Just quick update help needed, can we sort the data using transactionDate in ascending or descending order....? Any tips for the same – Sagar Feb 18 '21 at 10:43
  • You can, simply search sorting dictionary keys :) That should do the job, lemme check if I can find already answered question – Sandeep Bhandari Feb 18 '21 at 10:45
  • `let sortedKeys = Array(dictionary.keys).sorted(<)` take a look at https://stackoverflow.com/questions/25377177/sort-dictionary-by-keys from now on use `sortedKeys.count` for number of section and use `dict[sortedKeys[indexPath.section]]?.count` for number of cells in section finally use `dict[sortedKeys[indexPath.section]][indexPath.row]` probably to return cell, none of these are compiled codes typed directly in comment so might have compilation error :) – Sandeep Bhandari Feb 18 '21 at 10:45
  • @ Sandeep Bhandari :- thanks, I got the codes, will do necessary modifications and I am good to go. Thanks for help. – Sagar Feb 18 '21 at 10:57
  • @sagar tell me here, you can convert this to a chat if you want, adding duplicate questions will not help anybody – Sandeep Bhandari Feb 19 '21 at 09:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228940/discussion-between-sagar-and-sandeep-bhandari). – Sagar Feb 19 '21 at 09:40