0

I have this problem. I have this vector with two fields for position:

fecha:  9/10/2017
turno:  0
fecha:  9/10/2017
turno:  6
fecha:  9/10/2017
turno:  17
fecha:  10/10/2017
turno:  17

And for order this I am using this code:

self.TodasMisCitas.sort {
    if ($0.fecha as Date == $1.fecha as Date) {
        return ($0.turno) > ($1.turno)
    }
    return ($0.fecha) > ($1.fecha)
}

This is the output:

i don't upload image :( see in this link please

The correct output for me will be:

martes 10/10/2017
lunes 09/10/2017 a las 10:00
lunes 09/10/2017 a las 11:30
lunes 09/10/2017 a las 16:00

Anybody know what's happening?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yayo
  • 63
  • 2
  • 10
  • So how exactly do you want the sort to work? – Alexander Oct 08 '17 at 16:00
  • When you compare the dates with == maybe you should take into account the day not the entire date. – TomCobo Oct 08 '17 at 16:15
  • 1
    Explain what is exactly `fecha`. Your code should work, but if `fecha` doesn't hold hours/minutes (while a `Date` object does), but just the day it won't. And you mean that if the day/hour/minute are the same, then you check the `turno`, right? – Larme Oct 08 '17 at 16:30

1 Answers1

0

When you compare the dates with == maybe you should take into account the day not the entire date. I couldn't compile but I think it should be something like this:

self.TodasMisCitas.sort {
    if Calendar.current.compare($0.fecha, to: $1.fecha, toGranularity: .day) == .orderedSame {
        return ($0.turno) > ($1.turno)
    }
    return ($0.fecha) > ($1.fecha)
}
TomCobo
  • 2,886
  • 3
  • 25
  • 43