Dart, Compareto two situations in same time on list items - flutter

I have formatted dateFormat items, I mean : 11-08, 22-12 "dd-MM" items in a list. I want to sort them from the nearest date to farthest date. I did use compareTo like that (tumKisiler is a list)
'''
tumKisiler.sort((a, b) {
return a.siralamaDogumTarihi
.substring(3, 5)
.compareTo(b.siralamaDogumTarihi.substring(3, 5));
'''
but that is only sort by months. I want to sort by days also.
The output must be look like that :
13-08
26-08
17-09
31-12
How can I do that? I am new here, I hope could explain.

Related

Make a list with the quarter and year based on a date range of quarters KDB+/Q

I have a list of date ranges for the past 8 quarters given by the below function
q) findLastYQuarters:{reverse("d"$(-3*til y)+m),'-1+"d"$(-3*-1+til y)+m:3 bar"m"$x}[currentDate;8]
q) findLastYQuarters
2020.01.01 2020.03.31
2020.04.01 2020.06.30
2020.07.01 2020.09.30
2020.10.01 2020.12.31
2021.01.01 2021.03.31
2021.04.01 2021.06.30
2021.07.01 2021.09.30
2021.10.01 2021.12.31
I need to produce a separate list that labels each item in this list by a specific format; the second list would need to be
1Q20,2Q20,3Q20,4Q20,1Q21,2Q21,3Q21,4Q21
This code needs to be able to run on it's own, so how can I take the first list as an input and produce the second list? I thought about casting the latter date in the range as a month and dividing it by 3 to get the quarter and extracting the year, but I couldn't figure out how to actually implement that. Any advice would be much appreciated!
I'm sure there are many ways to solve this, a function like f defined below would do the trick:
q)f:{`$string[1+mod[`month$d;12]%3],'"Q",/:string[`year$d:x[;0]][;2 3]}
q)lyq
2020.01.01 2020.03.31
2020.04.01 2020.06.30
2020.07.01 2020.09.30
2020.10.01 2020.12.31
2021.01.01 2021.03.31
2021.04.01 2021.06.30
2021.07.01 2021.09.30
2021.10.01 2021.12.31
q)f lyq
`1Q20`2Q20`3Q20`4Q20`1Q21`2Q21`3Q21`4Q21
Figured it out.
crop:findLastYQuarters;
crop[0]:crop[0][1];
crop[1]:crop[1][1];
crop[2]:crop[2][1];
crop[3]:crop[3][1];
crop[4]:crop[4][1];
crop[5]:crop[5][1];
crop[6]:crop[6][1];
crop[7]:crop[7][1];
labels:()
labelingFunc:{[r] temp:("." vs string["m"$r]); labels,((string(("J"$temp[1])%3)),"Q",(temp[0][2,3])};
leblingFunc each crop;
labels

How to reduce code in Swift when dealing with variables and if statements?

I am currently building an app to teach myself Swift so am still very new. I’ve encountered a problem. The app is a timetable creator. I have up to twelve subjects that all have points. These points are then spread across days. I have a concept for code that will allocate the points fine using loops but wondered whether there was something to reduce the amount of code rather than, what I currently have, something like this for each subject:
subject1.monpts = 20
subject1.tuepts = 20
subject1.wedpts = 20
subject1.thurpts = 20
subject1.fripts = 20
subject1.satpts = 20
subject1.sunpts = 20
This, x12, is a lot of code. But I need it so that each subject has an option for points for each day of the week (which the app will then allocate time based on these points). To print the timetable, I am putting each subjectX.daypts together but this means I’m writing out 12 subjects for each day.
I also need to only display something if the variable actually has a value. I plan to do this using if statements but that means writing a statement for every single variable which at the moment is 12 x 7 = 48! E.g. (ignore the formating - just for concept)
if subjects1.monpts = 0 {
subjects1monLabel = isHidden.false
// or just a print / don't print statement
}
I feel as if I'm missing an easier way to do this!
Here is a picture that explains the concept a bit better:
If you want to save information about those fields you can have a dictionary with keys of a enum and values of ints like so:
enum WeekDay: CaseIterable {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
struct Subject {
var pointsForWeekDay: [WeekDay: Int]
}
Now you could do:
var pointsForWeekDay = Dictionary(uniqueKeysWithValues:
WeekDay.allCases.map { weekDay in
return (weekDay, 20)
}
)
var subject = Subject(pointsForWeekDay: pointsForWeekDay)
CaseIterable allows you to access all values of your enum.
Map takes every weekDay and creates an array of tuples with your Int values.
And finally you can combine that to have a complete Dictionary with uniqueKeysWithValues initializer, which takes an array of the produced tuples.
Your whole vision of how to organize this material is upside down. Start by thinking about what all your subjects have in common: points for each of the seven days, label hidden for each of the seven days, and so forth. Now incorporate that into a type (a struct): Subject. Now instead of subjects1..., subjects2... and so forth, you have an Array of Subject.
So: any time you have variables named with a number, that should be an array instead. Any time you have clumps of repeated concepts, that should be a type instead.
Even the notion of the seven days of the week could itself be condensed in the same way. If all we're talking about is points per day, an array of seven numbers would do.
So we'd end up with a skeleton like this:
struct Subject {
var dayPoints : [Int]
}
var myTwelveSubjects : [Subject]
...and you can build that out as more requirements come online, such as whether a day is hidden.

How to identify each of every elements is Before from next element in a List

I have a list that contain number of elements which are formatted as Local Date Time type as follow.
List(2017-06-25T00:00, 2017-06-25T00:05:13, 2017-06-25T00:11:11, 2017-06-25T00:17:39, 2017-06-25T00:24:44, 2017-06-25T00:32:33, 2017-06-25T00:41:11, 2017-06-25T01:01:03)
I want to check each and every elements of List is before from next element.As example first value of list is before from second value in list. Like wise I want to check for whole elements inside list.Can any one help me.
This will test if a List of well-formatted date strings are all in chronological order.
import java.time.LocalDateTime
val dates: List[String] = List( "2017-06-25T00:00"
, "2017-06-25T00:05:13"
// etc.
, "2017-06-25T01:01:03"
)
dates.iterator
.map(LocalDateTime.parse)
.sliding(2)
.forall(x => x(0) isBefore x(1)) // returns true/false
The .iterator is included so that the date-strings can be parsed lazily, i.e. if one of the first dates is out of order, then the subsequent dates in the list don't have to be parsed at all.
Here is a method for checking that each string with a date in the sequence is earlier than the next (it assumes that you have a method called isBefore that compares two of these Strings
#tailrec
def isMonotouslyIncreasing(seq: Seq[String]): Boolean = {
if(seq.size <= 1) true
else if(isBefore(seq(0), seq(1))) isMonotouslyIncreasing(seq.tail)
else false
}

Scala : exclude some Dates

I have a list of dates that i want to ignore :
private val excludeDates = List(
new DateTime("2015-07-17"),
new DateTime("2015-07-20"),
new DateTime("2015-07-23")
)
But i always need to display four dates excluding my black Dates list and the weekends. So far with the following code, my counter is increased when i hit an ignored date and it make sens. So how i can jump to the next date until i hit 4 dates not in my black list and my Weekends ? Maybe with a while, but i dont know how to add it in my scala code :
1 to 4 map { endDate.minusDays(_)} diff excludeDates filter {
_.getDayOfWeek() match {
case DateTimeConstants.SUNDAY | DateTimeConstants.SATURDAY => false
case _ => true
}
}
You could use a Stream :
val blacklist = excludeDates.toSet
Stream.from(1)
.map(endDate.minusDays(_))
.filter(dt => ! blacklist.contains(dt))
.take(4)
.toList
In a quick and rough way I would do it like this
val upperLimit = 4 + excludeDates.length
(1 to upperLimit).map( endDate.minusDays ).filter( d => !excludeDates.contains(d) ).take(4)
In short you go from the end date backward at max the number of dates you need plus the size of the excluded dates, then you filter the sequence checking if the date is not the excluded list and finally you pick only the dates you need with .take( n )
Hope it helps :)

How to groupBy groupBy?

I need to map through a List[(A,B,C)] to produce an html report. Specifically, a
List[(Schedule,GameResult,Team)]
Schedule contains a gameDate property that I need to group by on to get a
Map[JodaTime, List(Schedule,GameResult,Team)]
which I use to display gameDate table row headers. Easy enough:
val data = repo.games.findAllByDate(fooDate).groupBy(_._1.gameDate)
Now the tricky bit (for me) is, how to further refine the grouping in order to enable mapping through the game results as pairs? To clarify, each GameResult consists of a team's "version" of the game (i.e. score, location, etc.), sharing a common Schedule gameID with the opponent team.
Basically, I need to display a game result outcome on one row as:
3 London Dragons vs. Paris Frogs 2
Grouping on gameDate let's me do something like:
data.map{case(date,games) =>
// game date row headers
<tr><td>{date.toString("MMMM dd, yyyy")}</td></tr>
// print out game result data rows
games.map{case(schedule,result, team)=>
...
// BUT (result,team) slice is ungrouped, need grouped by Schedule gameID
}
}
In the old version of the existing application (PHP) I used to
for($x = 0; $x < $this->gameCnt; $x = $x + 2) {...}
but I'd prefer to refer to variable names and not the come-back-later-wtf-is-that-inducing:
games._._2(rowCnt).total games._._3(rowCnt).name games._._1(rowCnt).location games._._2(rowCnt+1).total games._._3(rowCnt+1).name
maybe zip or double up for(t1 <- data; t2 <- data) yield(?) or something else entirely will do the trick. Regardless, there's a concise solution, just not coming to me right now...
Maybe I'm misunderstanding your requirements, but it seems to me that all you need is an additional groupBy:
repo.games.findAllByDate(fooDate).groupBy(_._1.gameDate).mapValues(_.groupBy(_._1.gameID))
The result will be of type:
Map[JodaTime, Map[GameId, List[(Schedule,GameResult,Team)]]]
(where GameId is the type of the return type of Schedule.gameId)
Update: if you want the results as pairs, then pattern matching is your friend, as shown by Arjan. This would give us:
val byDate = repo.games.findAllByDate(fooDate).groupBy(_._1.gameDate)
val data = byDate.mapValues(_.groupBy(_._1.gameID).mapValues{ case List((sa, ra, ta), (sb, rb, tb)) => (sa, (ta, ra), (tb, rb)))
This time the result is of type:
Map[JodaTime, Iterable[ (Schedule,(Team,GameResult),(Team,GameResult))]]
Note that this will throw a MatchError if there are not exactly 2 entries with the same gameId. In real code you will definitely want to check for this case.
Ok a soultion from Régis Jean-Gilles:
val data = repo.games.findAllByDate(fooDate).groupBy(_._1.gameDate).mapValues(_.groupBy(_._1.gameID))
You said it was not correct, maybe you just didnt use it the right way?
Every List in the result is a pair of games with the same GameId.
You could pruduce html like that:
data.map{case(date,games) =>
// game date row headers
<tr><td>{date.toString("MMMM dd, yyyy")}</td></tr>
// print out game result data rows
games.map{case (gameId, List((schedule, result, team), (schedule, result, team))) =>
...
}
}
And since you dont need a gameId, you can return just the paired games:
val data = repo.games.findAllByDate(fooDate).groupBy(_._1.gameDate).mapValues(_.groupBy(_._1.gameID).values)
Tipe of result is now:
Map[JodaTime, Iterable[List[(Schedule,GameResult,Team)]]]
Every list again a pair of two games with the same GameId