I have below list of folder paths
var allLeafDirPaths= Array(
"abfss://[email protected]/customer/Full/20200306/",
"abfss://[email protected]/customer/Full/20200318/",
"abfss://[email protected]/customer/Full/20200319/",
"abfss://[email protected]/customer/Full/20200504/",
"abfss://[email protected]/customer/Full/20201020/",
"abfss://[email protected]/customer/Full/20210302/",
"abfss://[email protected]/customer/Full/20220215/",
"abfss://[email protected]/customer/Full/20220216/",
"abfss://[email protected]/customer/Full/20220223/",
"abfss://[email protected]/customer/Full/20220301/"
)
I want to pick the latest folder as you can see it should be below one generated on 01-Mar-2022
abfss://[email protected]/customer/Full/20220301/"
I tried below code that works. But my code may not be that good. Can we do this in any other better way?
for(path <- allLeafDirPaths){
pathDatesString := path.substring(path.substring(0,path.lastIndexOf("/")).lastIndexOf("/") 1,path.length()-1)
pathDatesInt = pathDatesString.map(_.toInt)
maxPathDatesInt = pathDatesInt.reduceLeft(_ max _)
if(path.contains("/".concat(maxPathDatesInt.toString).concat("/"))){
finalPath = path
}
}
finalPathArray = Array("")
finalPathArray := finalPath
println("final path is")
println(finalPathArray.mkString("\n"))
CodePudding user response:
Use maxBy
or maxByOption
and maybe a Regex instead of the indexOf
stuff.
val DatePattern = ".*/(\\d )/$".r
def pathToDate(path: String): Option[Int] = path match {
case DatePattern(rawDate) => Some(rawDate.toInt)
case _ => None
}
allLeafDirPaths.maxBy(pathToDate)
// returns "abfss://[email protected]/customer/Full/20220301/"
Explanation
maxBy
finds the max from the sequence based on a "measurement function", e.g. you want to measure your paths by a date value, so you provide the pathToDate
method as your measurement function.
Scala provides a handy Regex
class and a convenience .r
method on String (implicitly via StringOps) that lets you construct a Regex from a String pattern. Regex implements unapplySeq(s: CharSequence)
, which lets it work as an "extractor object" in a match/case
block.