1

I have declared an Array like :

  var sessionsToDisplayTemp : [SessionData] = []

The value of sessionsToDisplayTemp is:

[<SessionData: 0x600001c16a30> (entity: SessionData; id: 0x8b2a8ba144e57f29 <x-coredata://0A752C13-D33E-4AE2-BE3E-7B856C0EE8D9/SessionData/p3> ; data: {

    startTime = "14:30";
    switchInGroup = FALSE;

}), <SessionData: 0x600001c16a80> (entity: SessionData; id: 0x8b2a8ba144f97f29 <x-coredata://0A752C13-D33E-4AE2-BE3E-7B856C0EE8D9/SessionData/p4> ; data: {

    startTime = "13:30";
    switchInGroup = TRUE;

}), <SessionData: 0x600001c16cb0> (entity: SessionData; id: 0x8b2a8ba144fd7f29 <x-coredata://0A752C13-D33E-4AE2-BE3E-7B856C0EE8D9/SessionData/p5> ; data: {
        startTime = "13:30";
    switchInGroup = FALSE;

})]

My problem is I have to check the Start time, if the start time will be same then have to hide from this array and this array should be in the order of Start Time. I mean the final array should be :

[<SessionData: 0x600001c16a30> (entity: SessionData; id: 0x8b2a8ba144e57f29 <x-coredata://0A752C13-D33E-4AE2-BE3E-7B856C0EE8D9/SessionData/p3> ; data: {

        startTime = "13:30";
        switchInGroup = TRUE;

    }), <SessionData: 0x600001c16a80> (entity: SessionData; id: 0x8b2a8ba144f97f29 <x-coredata://0A752C13-D33E-4AE2-BE3E-7B856C0EE8D9/SessionData/p4> ; data: {

        startTime = "14:30";
        switchInGroup = FALSE;

    })]

I have done the ascending part based on Start time using :

sessionsToDisplayTemp.sort(by: { $0.startTime?.compare(($1.startTime)!) == .orderedAscending })

Can anyone help me to achieve this. Thank you.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70

2 Answers2

0

This will create an array, out, that is sorted and without duplicates

var out = [SessionData]()
for data in sessionsToDisplayTemp.sorted(by: { $0.startTime < $1.startTime}) {
    if out.isEmpty {
        out.append(data)
    } else if let last = out.last, last.startTime != data.startTime {
        out.append(data)
    }
} 
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

You can create a new array, where you would append only the SessionData objects with unique startTime. To make it easy, you can use Set as mentioned by @Anbu.Karthik in the comments.

As a simulation of your SessionDataI use a Tuple

var sessionToDisplay : [(startTime: String, switchInGroup: Bool)] = [
  (startTime: "14:30", switchInGroup: false),
  (startTime: "13:30", switchInGroup: true),
  (startTime: "13:30", switchInGroup: false),
];

Then the code would be like this:

var uniqueTimes = Set<String>();
var newArr : [(startTime: String, switchInGroup: Bool)] = [];

//Add only the objects with unique startTime (first occurrence in original array)
for (st, sig) in sessionToDisplay{
  if(uniqueTimes.insert(st).inserted){
    newArr.append((st, sig));
  }
}

//Sort as you need
newArr.sort(by: { $0.startTime.compare(($1.startTime)) == .orderedAscending });
Nikita Kurtin
  • 5,889
  • 4
  • 44
  • 48